我需要为所有annotationView添加点击监听器。此侦听器打开另一个视图控制器。我写了这段代码:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let selected = (view.annotation as? CustomAnnotation) {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped(sender:)))
view.addGestureRecognizer(tapGestureRecognizer)
self.selectedAnnotation = selected
}
}
@objc func tapped(sender: UITapGestureRecognizer)
{
if let controller = DetailsController.storyboardInstance(){
if let selectedAnnotation = selectedAnnotation{
controller.selectedAnnotation = selectedAnnotation
mapView.deselectAnnotation(selectedAnnotation, animated: false)
}
self.present(controller, animated:true, completion:nil)
}
}
tapped
监听器打开另一个视图控制器。但是当我关闭这个视图控制器时,MkAnnotationView仍然被选中。怎么关闭它?有没有其他方法可以为所有MkAnnotationView添加回调?