检测calloutAccessoryControlTapped委托中哪个附件视图被点击

时间:2011-01-31 05:36:10

标签: iphone objective-c ios

我想检测是否已通过下面的委托方法点击了rightCalloutAccessoryView,我该怎么做?

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

2 个答案:

答案 0 :(得分:11)

calloutAccessoryControlTappedleftCalloutAccessoryView的点击操作都会触发

rightCalloutAccessoryView方法。要区分附件视图,您可以在创建附件视图时为其设置tag。在calloutAccessoryControlTapped方法中,您可以检查代码值并根据代码值执行相应的操作。

例如,假设您已分别为12附件视图的tags设置了leftright。然后,您的calloutAccessoryControlTapped方法将类似于以下内容,

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

    if ([control tag] == 1) {

        // Left Accessory Button Tapped

    } else if ([control tag] == 2) {

        // "Right Accessory Button Tapped
    }
}

答案 1 :(得分:0)

我会使用以下实现:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    switch control {
    case let left where left == view.leftCalloutAccessoryView:
        // tap on left
        break
    case let right where right == view.rightCalloutAccessoryView:
        // tap on right
        break
    default:
        break
    }
}

由于switchleftCalloutAccessoryView是选项,rightCalloutAccessoryView语法比平常更复杂,但它避免使用标记。