Store data in MKAnnotation?

In the viewForAnnotation method, you can access your ArtPiece properties but to avoid compiler errors and warnings, you’ll need to first cast the annotation parameter to your custom class. The annotation parameter in that method is just defined as a id<MKAnnotation> so the compiler won’t know about the ArtPiece-specific properties (until you tell it that is an instance of ArtPiece).

You’ll need something like this:

ArtPiece *artPiece = (ArtPiece *)annotation;
NSString *artist = artPiece.artist;

Edit:
When an annotation view’s callout button is pressed, the map view calls the calloutAccessoryControlTapped delegate method. This method gets passed the MKAnnotationView in the view parameter. The annotation object itself is in the annotation property of the view parameter. You can cast that object to your custom class to access your ArtPiece properties:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    ArtPiece *artPiece = (ArtPiece *)view.annotation;
    NSString *artist = artPiece.artist;
}

In other words, the callout button handler doesn’t need to access the original array. It gets a reference to the actual ArtPiece object itself.

Leave a Comment