首页 > 解决方案 > 我的 MKMapAnnotation 的扩展工作了 1/3 次,有什么办法可以修复它,让它一直工作吗?

问题描述

所以我正在制作一个需要在 swift 中使用 MKMapAnnotations 的应用程序,为此我使用了一个扩展程序,该扩展程序将每个注释自定义为具有一个按钮、一个自定义图像和一个背景。所有这些都在第一次尝试时有效,但是,对于第二次和第三次尝试,它显示默认注释,没有按钮或注释功能,但在第四次尝试时它有效(之后每三次尝试)。为此,我登录和退出我的应用程序并重新加载 MapView。这是什么原因/我该如何解决?这是扩展名。

这是我第一次在这里发帖,所以任何有关如何帮助您帮助我的建议也将不胜感激:)

extension MapViewController: MKMapViewDelegate {
    // 1

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        // 2
        guard let annotation = annotation as? Artwork 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
            view.glyphImage = UIImage(named: "TiffinIcon")
            view.markerTintColor = UIColor.orange
        }
        else {
            // 5
            view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            view.glyphImage = UIImage(named: "TiffinIcon")
            view.markerTintColor = UIColor.orange
            //view.tag = counter
            let btn = UIButton(type: .detailDisclosure)
            btn.setImage(UIImage(named: "TiffinIcon"), for: .normal)
            view.rightCalloutAccessoryView = btn
            //view.tag = counter
            print("counter tagged:\(counter)")


        }
        return view
    }
    //Button Clicked Function
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if let annotation = view.annotation as? Artwork {
            print("This is the \(annotation.tag)")
            newTag = annotation.tag
            performSegue(withIdentifier: "successfulSegue", sender: self)
        }
    }
}

标签: iosswiftxcodemkannotationmkannotationview

解决方案


推荐阅读