[英]How to detect button press on MKMapview's annotation?
I recently added an info button to each of my annotations with the following code: 我最近使用以下代码为每个注释添加了一个信息按钮:
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
}
}
My question is how do I go about detecting when this button is pressed? 我的问题是如何检测何时按下此按钮?
You need to set the delegate 您需要设置委托
self.mapView.delegate = self
And implement 并实施
func mapView(_ mapView: MKMapView,
annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let anno = view.annotation as! AnnoatioClassName
// do segue here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.