【问题标题】:Problem with delegates removing annotation代表删除注释的问题
【发布时间】:2021-02-26 16:43:42
【问题描述】:

我有两个屏幕。第一个(firstViewController)有一个带有UITapGestureRecognizer 的mapView。当用户点击屏幕时,会在地图上添加注释并显示第二个屏幕(secondViewController)。

当用户关闭 secondViewController 并返回到第一个时,注释应该被删除。我知道我必须使用委托,但我就是无法让它发挥作用。

这是我现在的代码:

class firstViewController: UIViewController, AnnotationDelegate {
    
    let mapView = MKMapView()

    var temporaryPinArray = [MKPointAnnotation]()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(mapView
        
        let gesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        mapView.addGestureRecognizer(gesture)
        secondVC.annotationDelegate = self
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        mapView.frame = view.bounds
    }
    
    @objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) {
        let location = gestureReconizer.location(in: mapView)
        let coordinates = mapView.convert(location, toCoordinateFrom: mapView)
        mapView.removeAnnotations(mapView.annotations)       
        
                let pin = MKPointAnnotation()
        pin.coordinate = coordinates
        
        temporaryPinArray.removeAll()
        temporaryPinArray.append(pin)
        mapView.addAnnotations(temporaryPinArray)

                // Present secondViewController
        let secondVC = SecondViewController()
        panel.set(contentViewController: secondVC)
        panel.addPanel(toParent: self)
    }

        func didRemoveAnnotation(annotation: MKPointAnnotation) {
        mapView.removeAnnotation(annotation)
    }
}

第二个视图控制器

    protocol AnnotationDelegate {
        func didRemoveAnnotation(annotation: [MKPointAnnotation])
    }

class SecondViewController: UIViewController {
    
    var annotationDelegate: AnnotationDelegate!
    
    let mainVC = firstViewController()
    
    let closeButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = .grey
        button.layer.cornerRadius = 15
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(closeButton)
        
        closeButton.addTarget(self, action: #selector(dismissPanel), for: .touchUpInside)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        closeButton.frame = CGRect(x: view.frame.width-50, y: 10, width: 30, height: 30)
    }
    
    @objc func dismissPanel() {
        self.dismiss(animated: true, completion: nil)
        annotationDelegate.didRemoveAnnotation(annotation: mainVC.temporaryPinArray)
    }
}

非常感谢您的帮助!

【问题讨论】:

    标签: swift xcode annotations mapkit


    【解决方案1】:

    您在SecondViewController 内创建了firstViewController 的新实例。此实例与实际的第一个实例无关:

    let mainVC = firstViewController()
    

    这意味着temporaryPinArray 也不同。所以不要传入这个不相关的数组...

    @objc func dismissPanel() {
        self.dismiss(animated: true, completion: nil)
        annotationDelegate.didRemoveAnnotation(annotation: mainVC.temporaryPinArray)
    }
    

    只需将函数更改为不带参数即可:

    protocol AnnotationDelegate {
        func didRemoveAnnotation() /// no parameters
    }
    
    @objc func dismissPanel() {
        self.dismiss(animated: true, completion: nil)
        annotationDelegate.didRemoveAnnotation() /// no parameters
    }
    

    firstViewControllerdidRemoveAnnotation 中,引用实际的temporaryPinArray

    func didRemoveAnnotation() {
        mapView.removeAnnotations(temporaryPinArray) /// the current array
    }
    

    【讨论】:

    • 我已经按照你说的做了,但是当我尝试关闭 secondViewController 时,应用程序崩溃了。它在 annotationDelegate.didRemoveAnnotation() 的行上说:“致命错误:在隐式展开可选值时意外发现 nil”
    • @danicode 确保你在做secondVC.annotationDelegate = self。另外,使用? 执行annotationDelegate?.didRemoveAnnotation() 以避免强制展开/崩溃
    • 我真的认为我已经完成了所有设置。但是当我关闭第二个视图控制器时,图钉仍在地图上。我不知道问题出在哪里;(
    • @danicode 刚刚意识到。您可以发布您如何呈现secondVC 的代码吗?您需要在那里设置委托。
    • 非常感谢!!我现在意识到问题在于我在 viewDidLoad 中设置了委托,而不是呈现secondVC 的函数。我非常感谢你,因为我现在试图解决这个问题太多时间了。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2011-05-23
    相关资源
    最近更新 更多