detecting which accessory view is tapped in calloutAccessoryControlTapped delegate
I would like to detect if the rightCalloutAccessoryView has be开发者_开发问答en tapped via the delegate method below, how can I do that?
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)contro
calloutAccessoryControlTapped
method will be triggered for the tap action on both leftCalloutAccessoryView
and rightCalloutAccessoryView
. To distinguish the accessory views, you can set tag
for both the accessory views while you create them. And in your calloutAccessoryControlTapped
method, you can check the tag value and do the respective action depending on the tag value.
For example, consider you have set 1
and 2
for the tags
of your left
and right
accessory view respectively. Then your calloutAccessoryControlTapped
method will look something like the following,
- (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
}
}
I would use the following implementation:
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
}
}
The switch
syntax is a bit more complicated than usual due to leftCalloutAccessoryView
and rightCalloutAccessoryView
being optionals, but it avoids using tags.
精彩评论