I have a mapView with annotations with detail disclosure that when clicked on, moves to a new viewController. Then when the user selects to go back to the mapView from the detail disclosure, I want the mapView to have the same view that the user had before clicking on the detail disclosure. Below is the code I have set up so far, however, when the user clicks back from the detail disclosure, the map zooms out. I am not sure how to recreate the previous view.
Detail Disclosure VC:
// here by segueing, I want to communicate to the MapView VC that the map should remain the same
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "backButtonTapped" )
{
let destinationVC = segue.destination as! TabBarVC
mapStatus = "wentBack"
print("The value of mapStatus after seguing is \(mapStatus)")
destinationVC.selectedIndex = 1
}
}
MapView VC:
override func viewDidAppear(_ animated: Bool) {
// here if the mapStatus is not the correct value (the user never used the detail disclosure), the map will just snap onto the current location
if mapStatus == "wentBack" {
print("Do not update location")
// assuming appropriate code is placed here but I am not sure what it is
} else {
print("Updated Current Location")
let center = CLLocationCoordinate2D(latitude: self.lastLocation.coordinate.latitude, longitude: self.lastLocation.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
map.setRegion(region, animated: true)
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if #available(iOS 8.0, *) {
locationManager.requestAlwaysAuthorization()
} else {
// Fallback on earlier versions
}
let noLocation = CLLocationCoordinate2D()
let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
map.setRegion(viewRegion, animated: false)
}
}
So it turns out that the map does not persist because viewDidLoad is being called when the user hits back on the detail disclosure (using a prepareForSegue). I used
@IBAction func backButtonTapped(_ sender: UIBarButtonItem) {
self.dismiss(animated: true)
mapStatus = "wentBack"
}
In order to just dismiss the viewController, and only calling viewDidAppear and not viewDidLoad.