我在我的annotationView的两侧都有两个按钮,希望它们根据单击哪个来执行不同的方法。我对第一个按钮使用了calloutAccessoryControlTapped,但不知道如何为第二个按钮(button2)指定方法。任何帮助将不胜感激。
谢谢
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Define a reuse identifier just like with tables or collection views
let identifier = "Capital"
if annotation is Capital {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
// If it isn't able to find a reusable view, create a new one using MKPinAnnotationView and sets its canShowCallout property to true. This triggers the popup with the city name.
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let button = UIButton(type: .detailDisclosure)
annotationView!.rightCalloutAccessoryView = button
let button2 = UIButton(type: .contactAdd)
annotationView?.leftCalloutAccessoryView = button2
} else {
annotationView!.annotation = annotation
}
return annotationView
}
return nil
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let capital = view.annotation as! Capital
let placeName = capital.title
let placeInfo = capital.info
// let fullImage = UIImage(named: "fullHeart")
// let emptyImage = UIImage(named: "emptyHeart")
let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
答案 0 :(得分:0)
尝试以下代码
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
// rightCalloutAccessoryView tapped
} else if control == view.leftCalloutAccessoryView {
// leftCalloutAccessoryView tapped
}
}