ios MKMapView:在注释图钉上获取点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1599406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:33:37  来源:igfitidea点击:

MKMapView: Get clicked event on annotation pin

ioscocoa-touchuitableviewmkmapview

提问by squeezer123

I am using an MKMapViewcontaining a couple of MKAnnotationpins.
Above the map I am showing a UITableViewwith detailed information of the MKAnnotationpins.

我正在使用一个MKMapView包含几个MKAnnotation引脚的。
在地图上方,我显示了引脚的UITableView详细信息MKAnnotation

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  

回答by software evolved

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

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

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

回答by squeezer123

you can use an Observer for Selected-Event:

您可以为 Selected-Event 使用 Observer:

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

回答by Will Harris

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

我还没有在 MapKit 中看到一种简单的方法来做到这一点。mapView:annotationWasTapped:代表上没有。

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.

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