0

I have 2 storyboards. I need to switch between them. The first storyboard has a map view controller. When I switch to the second storyboard, I change rootViewController, as below

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()

But when I open the first storyboard I see all annotations as earlier. How can I remove cache from map?

Wez
  • 10,555
  • 5
  • 49
  • 63
  • let annotations = mapView.annotations; mapView.removeAnnotations(annotations) – El Tomato Mar 29 '19 at 13:22
  • Possible duplicate of [How to delete all Annotations on a MKMapView](https://stackoverflow.com/questions/3027392/how-to-delete-all-annotations-on-a-mkmapview) – AamirR Mar 29 '19 at 13:48

2 Answers2

0

its because the mapviewController is still exist with the map annotations just remove all annotations before switching below is the required code:-

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

Only to remove the annotations during the transition to second storyboard, you could try

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.mapView.removeAnnotations(mapView.annotations)
}
AamirR
  • 11,672
  • 4
  • 59
  • 73
iOS
  • 3,526
  • 3
  • 37
  • 82