切换故事板后如何清除所有地图?

问题描述 投票:0回答:2

我有2个故事板。我需要在它们之间切换。第一个故事板有一个地图视图控制器。当我切换到第二个故事板时,我改变了rootViewController,如下所示

let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let storyboard = UIStoryboard(name: "LoginController", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "LoginController")
appDelegate.window?.rootViewController = viewController
appDelegate.window?.makeKeyAndVisible()

但是,当我打开第一个故事板时,我看到所有注释都是早先的。如何从地图中删除缓存?

ios swift mkmapview
2个回答
0
投票

因为mapviewController仍然存在,地图注释只是删除所有注释,然后在下面切换是必需的代码: -

for (int i =0; i < [mapView.annotations count]; i++) { 
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {                      
         [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; 
       } 
    }

0
投票

只有在转换到第二个故事板的过程中删除注释,您才可以尝试

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.mapView.removeAnnotations(mapView.annotations)
}
© www.soinside.com 2019 - 2024. All rights reserved.