我似乎无法访问 mapView:viewForAnnotation 委托(delegate)方法中的自定义 MKAnnotation 属性。据我了解,该方法将注释作为值。我正在尝试针对传入的每个注释的属性。
代码
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//This actually returns a proper coordinate
NSLog(@"%f", annotation.coordinate.latitude);
//This gives me an error: Property 'annotationMarker' not found on object of type '__strong id<MKAnnotation>'
NSLog(@"%@", annotation.annotationMarker);
MKAnnotationView *testPin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
testPin.image = [UIImage imageNamed:[[testArray objectAtIndex:1]annotationMarker]];
return testPin;
}
我想也许自定义属性设置不正确,但我可以在代码的其他部分记录这些注释的属性值。
我是否犯了某种语法错误?这个委托(delegate)方法是否以某种方式去除了自定义属性?
最佳答案
您需要转换为您的自定义 MKAnnotation
-conformant 类,例如
CustomAnnotation *customAnnotation = (CustomAnnotation *)annotation;
NSLog(@"%@", customAnnotation.annotationMarker);
关于iOS 无法访问 MKAnnotation 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21321417/