重新注册一个 AnnotationView

Re-registering an AnnotationView

AB 是不同种类的 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 从 A 更改为 B

@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?

我明白了。我在 MKAnnotation class 中创建了一个布尔值,并根据该布尔值在 annotationView A 中配置了注释属性。当我点击 UISwitch 时,我循环遍历所有注释对象,切换它们的布尔值,然后重新注册 annotationView,然后加载它们。