如何检测 MKMapview 注释上的按钮按下?



我最近使用以下代码在每个注释中添加了一个信息按钮:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation { return nil }
        if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "") {
            annotationView.annotation = annotation
            return annotationView
        } else {
            let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:"")
            annotationView.isEnabled = true
            annotationView.canShowCallout = true
            let btn = UIButton(type: .detailDisclosure)
            annotationView.rightCalloutAccessoryView = btn
            return annotationView
        }
    }

我的问题是我如何检测何时按下此按钮?

您需要设置委托

self.mapView.delegate = self

并实施

func mapView(_ mapView: MKMapView, 
   annotationView view: MKAnnotationView, 
     calloutAccessoryControlTapped control: UIControl) {
     let anno = view.annotation as! AnnoatioClassName
     // do segue here
}  

最新更新