I need to save the MapView
map type in UISegmentedControl
. The code just saves the selected map type in the UISegmentedControl
but not the map type on the MapView
. Here is my code:
@IBAction func standard(_ sender: UISegmentedControl) {
switch maptypes.selectedSegmentIndex {
case 0:
self.mapView.mapType = .hybrid
UserDefaults.standard.set(maptypes.selectedSegmentIndex, forKey: "selectedMapType")
case 1:
self.mapView.mapType = .standard
UserDefaults.standard.set(maptypes.selectedSegmentIndex, forKey: "selectedMapType")
case 2:
UserDefaults.standard.set(maptypes.selectedSegmentIndex, forKey: "selectedMapType")
self.mapView.mapType = .satellite
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let value = UserDefaults.standard.value(forKey: "selectedMapType"){
let selectedIndex = value as! Int
maptypes.selectedSegmentIndex = selectedIndex
}
}
Make it a function and call it in viewDidLoad
and action
func shared () {
switch maptypes.selectedSegmentIndex {
case 0:
self.mapView.mapType = .hybrid
UserDefaults.standard.set(maptypes.selectedSegmentIndex, forKey: "selectedMapType")
case 1:
self.mapView.mapType = .standard
UserDefaults.standard.set(maptypes.selectedSegmentIndex, forKey: "selectedMapType")
case 2:
UserDefaults.standard.set(maptypes.selectedSegmentIndex, forKey: "selectedMapType")
self.mapView.mapType = .satellite
default:
break;
}
}
//
@IBAction func standard(_ sender: UISegmentedControl) {
shared()
}
override func viewDidLoad() {
super.viewDidLoad()
if let value = UserDefaults.standard.value(forKey: "selectedMapType"){
let selectedIndex = value as! Int
maptypes.selectedSegmentIndex = selectedIndex
shared()
}
}