2014-01-23 58 views
1

我似乎无法访问mapView:viewForAnnotation委托方法内部的自定义MKAnnotation属性。据我所知,该方法将注释作为值。我努力的目标是获取传递的每个标注的属性。iOS无法访问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; 
} 

我想,也许自定义属性设置不正确,但我能来记录这些注释的属性值在代码的其他部分。

我在做某种语法错误吗?这种委托方法是否会以某种方式去除自定义属性?

回答

3

您需要投射到您的自定义MKAnnotation-compliant类,例如,

CustomAnnotation *customAnnotation = (CustomAnnotation *)annotation; 
NSLog(@"%@", customAnnotation.annotationMarker); 
+0

为什么这是必要的?为什么可以记录注释的坐标属性,而不是分配的定制属性? – sheepgobeep

+0

看看编译器告诉你什么。 协议具有坐标属性,但不具有您的annotationMarker属性。您的自定义注释类的确如此,这就是为什么您需要投入该类。 – Marco

+0

这很有道理。一般来说,我需要更好地掌握铸件。谢谢! – sheepgobeep