views:

623

answers:

1

Can anyone help me on how to use the

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

method.

I am trying to tell the annotation pins on my project appart and I can't figure out how. Every pin has a disclosure button but I can't figure out how the program will know which pin-button is being pushed.

+1  A: 

You can use a bunch of different techniques here.

For example, you could keep an ivar for each annotationView and do this:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
  if (view == myFirstAnnotationView) {
    //do something
  }
  if (view == mySecondAnnotationView) {
    //do something
  }
  if (view == myThirdAnnotationView) {
    //do something
  }
}

Or, you could use the tag value of the annotation view.

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
  if ([view.tag isEqualToString@"one"]) {

  }
  if ([view.tag isEqualToString@"two"]) {

  }
}

Or, you could subclass MKAnnotationView to add some behavior to it.

- (void)mapView:(MKMapView *)mapView annotationView:(MyMKAnnotationViewSubclass *)view calloutAccessoryControlTapped:(UIControl *)control {
 [view doAMethodIMadeWhenISubclassed];
}

None, of these is a complete solution, and it would be hard to provide one without beter understanding your application and design, but what they all have in common is changing the annotationView before you add it to the map in order to decide what to do after the delegate callback happens. I hope this leads you down the right path.

Brad Smith
I figured out how to go to the detail view that I've created but I was wondering if you can tell me how to tell the pins apart without any if statements. How can I know which button is pushed???
alecnash
Thanks, to tell you the truth I found it a bit easier to use a IBAction for the buttons.
alecnash