I have a map view that adds annotations more or less like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id
Dhanu A's solution in Swift 3:
func mapView(mapView: MKMapView, didSelectAnnotationView view:MKAnnotationView) {
let tapGesture = UITapGestureRecognizer(target:self, action:#selector(calloutTapped(sender:)))
view.addGestureRecognizer(tapGesture)
}
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
view.removeGestureRecognizer(view.gestureRecognizers!.first!)
}
func calloutTapped(sender:UITapGestureRecognizer) {
let view = sender.view as! MKAnnotationView
if let annotation = view.annotation as? MKPointAnnotation {
performSegue(withIdentifier: "annotationDetailSegue", sender: annotation)
}
}