首页 > 解决方案 > 重新注册一个 AnnotationView

问题描述

A并且B是不同种类的MKMarkerAnnotationView类。在我的 MapViewController 我A这样注册:

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self

    mapView.register(A.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

    for annotation in annotations {
        mapView.addAnnotation(annotation)
    }
}

我有一个UISwitch我想用来将注册的 AnnotationView 从更改AB.

@IBAction func didToggleSwitch(_ sender: Any) {

    mapView.register(B.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

    // remove all annotations
    mapView.removeAnnotations(mapView.annotations)

    //populate all annotations
    for annotation in annotations {
        mapView.addAnnotation(annotation)
    }

}

注册的视图没有被更新。任何想法为什么?

编辑:我注意到,当我缩小并平移地图时,一些注释会更新到新的 AnnotationView,而有些则不会。我怀疑这是由于使用了reuseIdentifier. 如何让所有注释符合新的 AnnotationView?

标签: swiftmkmapviewmkannotationview

解决方案


我想到了。我在我的类中创建了一个布尔值,并基于该布尔值MKAnnotation在 annotationView 中配置了注释属性。A当我点击 时UISwitch,我会遍历所有注释对象,切换它们的布尔值,然后重新注册 annotationView,然后加载它们。


推荐阅读