开发者

MKMapView: Get clicked event on annotation pin

I am using an MKMapView containing a couple of MKAnnotation pins.

Above the map I am showing a UITableView with detailed information of t开发者_运维百科he 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  


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


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
        }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜