我做了一个自定义的MKAnnotation类,MapLocation。我有没有问题设置或获取属性,除了在此方法来创建一个MKAnnotationView。我需要在这里做,因为它应该从注释索引查找的位置类型,然后选择一个家庭定义注解图像的annotationView之一。

在MapLocation.h和.M设置自定义的getter和setter无数次尝试后,我熬下来的地方,我甚至不能复制(强制性的)吸气,标题,其重命名为标题2,并尝试获取它的返回值。这是我的代码:

-(MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *placemarkIdentifier=@"Map Location Identifier";
NSString *str1=annotation.title;
NSString *str2=annotation.title2;
if([annotation isKindOfClass:[MapLocation class]]) {
    MKAnnotationView *annotationView=(MKAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
    if (annotationView==nil) {
        annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
    }
    else
        annotationView.annotation=annotation;


    return annotationView;
}
return nil;

}

在第4行,返回正确的标题,但第5行对所复制的方法调用的主题中得到的错误消息。

我没有在Xcode文档的样子,但我可能只是没有得到如何使这种方法看到它申报。奇怪的是,它看到标题吸气剂,但不是标题2的副本。

有帮助吗?

解决方案

尝试改变从点标记行这样:

NSString *str2=[annotation title2];

和误差应该消失。

这是怎么回事是编译器已被告知,注释是MKAnnotation。你知道什么它有其他的方法是无关紧要的事实;编译器是不是心理 - 所有它知道的是,注释如下MKAnnotation协议,仅此而已。它看到标题吸气剂的原因是怎么一回事,因为标题在MKAnnotation定义

您还可以通过使用的铸造修复此:

MapLocation *mapLocation = (MapLocation *)annotation;

现在,你可以说

NSString *str2=mapLocation.title2;

因为你告诉编译器mapLocation是MapLocation obejct。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top