0

I have an annotation class that conforms to the MKAnnotation protocol. The class has an init method which sets coordinates, title, and subtitle.

I create an array of these annotations and add them to a MKMapView the normal way by using addAnnotations:annotationArray.

Then in the MKMapViewDelegage method - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation I create a MKPinAnnotationView with a custom pin color, animatesDrop, canShowCallout. The callout has a UIButton of type UIButtonTypeDetailDisclosure.

When I run the application and click on a pin on the map I get the expected results which is the callout with title, subtitle, and the detailDisclosure button.

The thing I'm trying to figure out is how to pass data to a details page ([self.navigationController pushViewController:detailsController animated:YES];) when the user clicks the detail disclosure button. I tried to modify my annotations class by adding an additional parameter to the init method however I cannot access that parameter in the - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control method. It seems I can only access title and subtitle from there by doing something like this [view.annotation title].

I'm not sure what to do here, I can't seem to find any documentation on how to do this either...

Again. I want to pass an object to a detail view controller. The object would contain various data that would be used on the details page...

PS - I'm using Xcode 4.2 w/ ARC.

ElasticThoughts
  • 3,417
  • 8
  • 43
  • 58

1 Answers1

3

In the calloutAccessoryControlTapped, the annotation reference is the exact annotation object you created and added but the reference is typed generically as id<MKAnnotation>.

To access the custom properties in your annotation class, you can first cast it:

YourAnnotationClass *myAnn = (YourAnnotationClass *)view.annotation;

//now you can explicitly reference your custom properties...
NSLog(@"myAnn.customProp = %@", myAnn.customProp);

Make sure the "customProp" is defined as a property in your annotation class.

  • 2
    Yeah - but be SURE to check the class type of `myAnn` first. If the user taps the "Current Location" annotation you will also get this message - and if you're assuming it's one of your classes, it will break with a method not implemented exception. – Steve Feb 25 '12 at 23:25
  • Thanks Anna, this worked beautifully! Also thanks to you Steve for pointing the current location issue out. I've implemented some additional code to prevent this error `if ([myMapAnnotation isKindOfClass:[MyMapAnnotation class]])` – ElasticThoughts Feb 25 '12 at 23:43