19

I am using an MKMapView containing a couple of MKAnnotation pins.
Above the map I am showing a UITableView with detailed information of the MKAnnotation pins.

My problem: When I select a pin, I would like to select the corresponding table cell. For this I would like to catch an event/delegate if the pin is selected. I am not talking about calling the callout accessory

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control  
Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
squeezer123
  • 191
  • 1
  • 1
  • 5

3 Answers3

42

Just an update to this -- in iOS 4 there are MKMapViewDelegate methods that can be used to track annotation selection and de-selection:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
dandan78
  • 13,328
  • 13
  • 64
  • 78
software evolved
  • 4,314
  • 35
  • 45
  • 4
    If you want to know if the user is clicking on an expanded/selected annotation you can check view.selected == YES in didSelectAnnotationView – nylund Jul 02 '12 at 11:33
3

you can use an Observer for Selected-Event:

[pin addObserver:self
      forKeyPath:@"selected" 
         options:NSKeyValueObservingOptionNew
         context:@"ANSELECTED"];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    NSString *action = (NSString*)context;

    if([action isEqualToString:@"ANSELECTED"]){

        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            // clicked on an Annotation
        }
        else {
            // Annotation disselected
        }
    }
}
1

I haven't seen a simple way to do this in MapKit. There's no mapView:annotationWasTapped: on the delegate.

One way to do it would be to provide your own annotation view subclass. The custom annotation view could capture the pin selection in setSelected:animated: or in a lower level event handler and pass that information up to your view controller.

Will Harris
  • 21,597
  • 12
  • 64
  • 64
  • This is the way I was doing it as well. Strange that Apple didn't provide any callback for that :/ – yonel Jan 20 '10 at 15:05