我需要在 UISegmentedControl
中保存 MapView
map 类型。该代码只是将所选 map 类型保存在 UISegmentedControl
中,而不是将 map 类型保存在 MapView
中。这是我的代码:
@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
}
}
最佳答案
让它成为一个函数并在viewDidLoad
和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()
}
}
关于swift - Userdefaults 保存 mapType UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51464668/