如何将回调添加到MKAnnotationView?

问题描述 投票:1回答:2

我需要为所有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添加回调?

ios swift mkannotationview
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.