MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked

If you are using storyboards and have a segue from your current scene to your destination scene, you can just respond to calloutAccessoryControlTapped. For example:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view];
}

Obviously, you can check for the type of annotation associated with that view, etc., if you have different segues you want to call for different annotation types.

And, of course, if you want to pass any information to that next scene in prepareForSegue like usual.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Details"])
    {
        MKAnnotationView *annotationView = sender;
        [segue.destinationViewController setAnnotation:annotationView.annotation];
    }
}

As you can see, I pass the annotation object to my next view. If you have additional fields in your JSON structure that are associated with each annotation, one easy solution is to add properties to your custom view associated for each of the fields you want to track. Just go to the .h for your annotation custom class and add the properties you need. And then when you create your custom annotations (to be added to the map), just set these properties, too. Then, when you pass this annotation to the next view controller, all of your desired properties will be available there.


Clearly, if you’re using NIBs, just do the NIB equivalents of whatever you want for instantiating the next view controller, setting whatever properties you want, and either pushing to it or presenting it modally, e.g.:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    DetailsViewController *controller = [[DetailsViewController alloc] initWithNibName:nil
                                                                                bundle:nil];
    controller.annotation = annotationView.annotation;
    [self.navigationController pushViewController:controller animated:YES]; // or use presentViewController if you're using modals
}

Leave a Comment