【问题标题】:How to set up alerts from Annotation Callout Xcode 9如何从 Annotation Callout Xcode 9 设置警报
【发布时间】:2019-06-24 09:28:26
【问题描述】:

我试图让注释标注中的按钮在按下时发出警报。编译器不会抛出任何错误,但程序似乎永远不会进入那个特定的 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)
}
}

【问题讨论】:

  • 你设置mapView.delegate = self了吗?
  • 是的,我在 viewDidLoad() 中设置了

标签: swift mapkit xcode9 mapkitannotation


【解决方案1】:

检查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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2022-09-26
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多