以下のコードで実行してもユーザーデフォルトが機能しません。
シュミレーターを立ち上げUIswitchをオフにし
それから
シュミレーターを停止しまた立ち上げると
UIswitchはオンになったままになっています。
なにがいけないのでしょうか?
swift
1import UIKit 2 3var bool1 = true 4 5class ViewController: UIViewController { 6 7 8 @IBOutlet weak var switch1: UISwitch! 9 @IBAction func switchA1(_ sender: UISwitch) { 10 if switch1.isOn == true{ 11 bool1 = true 12 let ud = UserDefaults.standard 13 ud.set(bool1, forKey: "bool1Key") 14 ud.synchronize() 15 } 16 else{ 17 bool1 = false 18 let ud = UserDefaults.standard 19 ud.set(bool1, forKey: "bool1Key") 20 ud.synchronize() 21 } 22 print("(bool1)") 23 24 } 25 override func viewDidLoad() { 26 super.viewDidLoad() 27 // Do any additional setup after loading the view, typically from a nib. 28 print("(bool1)") 29 } 30 override func viewWillAppear(_ animated: Bool) { 31 switch1.isOn = bool1 32 } 33 34 override func didReceiveMemoryWarning() { 35 super.didReceiveMemoryWarning() 36 // Dispose of any resources that can be recreated. 37 } 38 39 40}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/12 06:04