Расширения для моей MKMapAnnotation работают 1/3 раза, есть ли способ исправить это, чтобы он работал все время? - PullRequest
0 голосов
/ 12 апреля 2020

Итак, я делаю приложение, которое требует использования MKMapAnnotations в swift, чтобы сделать это, я использовал расширение, которое настраивает каждую аннотацию, чтобы она имела кнопку, пользовательское изображение и фон. Все это работает с первой попытки, однако для второй и третьей попыток это показывает аннотацию по умолчанию без кнопок или функциональных возможностей для аннотаций, но с четвертой попытки это работает (и каждая третья попытка после). Для этого я выполняю вход и выход из своего приложения и перезагружаю 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)
        }
    }
}
...