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  
4

3 回答 3

42

只是对此的更新——在 iOS 4 中有 MKMapViewDelegate 方法可用于跟踪注释选择和取消选择:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
于 2011-03-14T18:19:05.730 回答
3

您可以对选定事件使用观察者:

[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
        }
    }
}
于 2009-11-10T08:46:01.793 回答
1

我还没有看到在 MapKit 中执行此操作的简单方法。mapView:annotationWasTapped:代表上没有。

一种方法是提供您自己的注释视图子类。自定义注释视图可以在较低级别的事件处理程序中或在其中捕获引脚选择,setSelected:animated:并将该信息传递给您的视图控制器。

于 2009-10-21T21:04:39.040 回答