Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top