swift - 如何从 Annotation Callout Xcode 9 设置警报

标签 swift mapkit xcode9 mapkitannotation

我正在尝试让注释标注中的按钮在按下时发出警报。编译器没有抛出任何错误,但程序似乎从未进入那个特定的 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 的额外参数。您的方法需要与委托(delegate)方法的签名完全匹配。

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

关于swift - 如何从 Annotation Callout Xcode 9 设置警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54451784/

相关文章:

swift - swift 是否具有像 Kotlin 那样的标准(范围)功能?

ios - 放置 UIView 覆盖 TabBar 和 NavBar

ios - 快速比较 NSDates

iphone - 使用 MapKit 显示特定区域

ios - (ios-charts)在不使用 Storyboard的情况下无法在 UICollectionView 中显示折线图

ios - 带导航栏的 MKMapView 内容插入

ios - 如何将来自 MapKit 注释的信息发送到新的 View Controller

ios - 尝试将 Crashlytics 更新到 v3.10.3 时没有此类模块错误

swift - 如何将 swift 从 3.1 更新到 3.2,而不是 4.0

swift - 运行 ibtool 时出现异常 : *** Collection was mutated while being enumerated. macOS