2014-01-23 55 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