swift - 如何从 Annotation Callout 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)
}
推荐阅读
- node.js - 使用“meteor npm install”命令时系统找不到指定的文件
- angular - 角度共享数据
- javascript - 在 ruby erb 中重定向后点击不起作用
- google-sheets-api - 如何获取 google sheet api 凭据(客户端机密、客户端 ID 等)并以编程方式通过浏览器进行授权?
- xamarin - 如何在 tvos 应用程序中连接按钮和集合视图?
- openssl - 安装 Openssl 后 Cent OS 8 重定位错误
- tsql - 如果在做东西时重复相同的功能,如何只显示一次文本单元?
- java - 无法在 android 中创建 ViewModel 的实例
- java - 观察者模式如何以及为什么可以实现 MVC 架构风格?
- flutter - 未来列表视图 - BoxConstraints 强制无限宽度