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

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
}

10-08 02:24