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

时间:2019-06-14 16:14:56

标签: swift xcode mapkit

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

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
        }
    }

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

1 个答案:

答案 0 :(得分:0)

您需要设置代表

self.mapView.delegate = self

并实施

func mapView(_ mapView: MKMapView, 
   annotationView view: MKAnnotationView, 
     calloutAccessoryControlTapped control: UIControl) {

     let anno = view.annotation as! AnnoatioClassName
     // do segue here
}