Using MKPinAnnotationView with UINavigationController


#1

I have an MKMapView as the root view for a UINavigationController. The map displays MKPinAnnotationView objects for several cities, and each MKPinAnnotationView has a disclosure button. When the user touches the button, I want to push a new view onto the screen (let’s call it DetailViewController) with information about that specific city.

What I can’t figure out is how that button can tell the DetailViewController which city it needs to display. What is the best approach for this situation? I suspect it has to do with target-action pairs and selectors, but I’m not clear enough yet on those subjects. I’d appreciate any help.

Thanks,
Jack


#2

Hi,

Have you looked at:

  • (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

in here you get passed the MKAnnotationView so you can then get to the associated annotation via its property.

You can then create an instance variable on your detailViewController and set it before pushing the view controller.

along the lines of:

  • (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
    [self.detailViewController setMyInstanceVariableTitle: view.annotation.title];
    [self.navigationController pushViewController:self.detailViewController animated:YES];
    }

Gareth


#3

Thanks Gareth, that’s exactly the method I needed! I’m not sure how I missed that. Here’s how I implemented it. For now, it just sets the title of the detailViewController.

[code]- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@“Location selected: %@”, view.annotation.title);

self.detailViewController = [[DetailViewController alloc] init];
self.detailViewController.navigationItem.title = view.annotation.title;

// TODO: Additional code to set up the view.

self.navigationController.navigationBarHidden = NO;
[self.navigationController pushViewController:self.detailViewController animated:YES];
[self.detailViewController release];

}[/code]