Swift 用户默认保存mapType UISegmentedControl
我需要将Swift 用户默认保存mapType UISegmentedControl,swift,mkmapview,uisegmentedcontrol,Swift,Mkmapview,Uisegmentedcontrol,我需要将MapView地图类型保存在UISegmentedControl中。代码仅将选定的地图类型保存在UISegmentedControl中,而不是MapView中的地图类型。这是我的密码: @IBAction func standard(_ sender: UISegmentedControl) { switch maptypes.selectedSegmentIndex { case 0: self.mapView.mapType =
MapView
地图类型保存在UISegmentedControl
中。代码仅将选定的地图类型保存在UISegmentedControl
中,而不是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()
}
}