如何从注释标注Xcode 9设置警报
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从注释标注Xcode 9设置警报相关的知识,希望对你有一定的参考价值。
我试图在注释标注中有一个按钮,按下时会发出警报。编译器不会抛出任何错误,但程序似乎永远不会进入该特定的mapView函数。
这是功能:
extension ViewController{
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// 2
guard let annotation = annotation as? parkingZone else { return nil }
// 3
let identifier = "marker"
var view: MKMarkerAnnotationView
// 4
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
// 5
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.markerTintColor = .black
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, vc: UIViewController, calloutAccessoryControlTapped control: UIControl) {
print("Button Press")
let alertController = UIAlertController(title: "Hello", message: "This will start alerts", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
vc.present(alertController, animated: true, completion: nil)
}
}
答案
检查你的calloutAccessoryControlTapped
方法的签名,你有一个vc: UIViewController
的额外参数。您的方法需要完全匹配委托方法的签名。
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("Button Press")
let alertController = UIAlertController(title: "Hello", message: "This will start alerts", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
以上是关于如何从注释标注Xcode 9设置警报的主要内容,如果未能解决你的问题,请参考以下文章