【问题标题】:Saving and retrieving a bool with UserDefaults使用 UserDefaults 保存和检索布尔值
【发布时间】:2017-02-27 05:02:31
【问题描述】:

我正在尝试将 bool 值从 UISwitch 保存到 UserDefaults,然后在另一个视图中检索它。但是,我尝试了多个教程和堆栈答案,但似乎没有一个有效。

这就是我保存它的方式:

class SettingsViewController: UITableViewController {

@IBOutlet weak var soundSwitchOutlet: UISwitch!

@IBAction func soundSwitch(_ sender: UISwitch) {

    UserDefaults.standard.set(soundSwitchOutlet.isOn, forKey: "sound")

}

这就是我试图在另一个视图中检索它的方式:

if let savedValue = UserDefaults.standard.bool(forKey: "sound") {
        boolValue = savedValue
    }

//this is inside viewDidLoad and "boolValue" was declared outside viewDidLoad//

由于某种原因,这段代码给了我错误,而我尝试过的所有方法都没有奏效。如何将 bool 保存到 UserDefaults 并在另一个视图中检索它?

编辑:我想我修复了第一部分。但是,我检索布尔值的方式似乎完全错误。另外:没有其他 stackExchange 答案可以响应我的要求,至少不会很快。

【问题讨论】:

  • UserDefaults.standard.set(sender.isOn, forKey: "sound")
  • 为键读取 bool 会返回一个 Bool(非可选)boolValue = UserDefaults.standard.bool(forKey: "sound")

标签: ios swift nsuserdefaults


【解决方案1】:

正如 Leo 在 cmets 中提到的 bool(forKey 返回一个非可选的 Bool。如果key不存在则返回false

原来如此

boolValue = UserDefaults.standard.bool(forKey: "sound")

不需要按照其他答案中的建议调用synchronize()。框架会定期更新用户默认数据库。

【讨论】:

  • 谢谢!开关的状态保持保存,但是当我尝试在另一个视图上使用布尔值时,它会卡在它开始的布尔值上。
  • 不要滥用UserDefaults作为临时存储或在控制器之间传递数据。
【解决方案2】:

这样做。

在你的first view controller

  • 创建到您的UISwitchIBoutlet 连接

  • 然后是您的UISwitch 的操作。所以最后,你的first view controller 应该是这样的。

导入 UIKit

class FirstViewController: UIViewController {


    @IBOutlet weak var myswitch: UISwitch! // Outlet connection to your UISwitch (just control+ drag  it to your controller)




    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    @IBAction func myswitchAction(_ sender: Any) { // Action for your UISwitch

        var myswitctBool : Bool = false // create a local variable that holds your bool value. assume that in the beginning your switch is offed and the boolean value is `false`

        if myswitch.isOn == true { // when user turn it on then set the value to `true`
            myswitctBool = true
        }
        else { // else set the value to false
            myswitctBool = false
        }


        // finally set the value to user default like this
        UserDefaults.standard.set(myswitctBool, forKey: "mySwitch")
        //UserDefaults.standard.synchronize() - this is not necessary with iOS 8 and later.


    }

}

第一个视图控制器结束

现在在您的第二个视图控制器中

  • 你可以得到userdefault的值,这是你在first view controller中设置的。我把它放在viewdidload 方法中,向你展示它是如何工作的。

导入 UIKit

class SecondViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()


            let myswitchBoolValuefromFirstVc : Bool = UserDefaults.standard.bool(forKey: "mySwitch")// this is how you retrieve the bool value


            // to see the value, just print those with conditions. you can use those for your things.
            if myswitchBoolValuefromFirstVc == true {
                print("true")
            }
            else {
                print("false")
            }


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()

        }




    }

希望对您有所帮助。祝你好运

【讨论】:

  • 是的 @EricAya 我们不再需要它从 iOS 8。接受,我将删除它。谢谢
【解决方案3】:

使用这行代码:

@IBAction func soundSwitch(_ sender: UISwitch) {
    UserDefaults.standard.set(soundSwitchOutlet.isOn, forKey: "sound")
}

代替:

@IBAction func soundSwitch(_ sender: UISwitch) {
    UserDefaults.standard.set(soundSwitchOutlet, forKey: "sound")
}

【讨论】:

    【解决方案4】:

    试试这个:

    @IBAction func soundSwitchs(_ sender: Any) 
    {
        UserDefaults.standard.set(soundSwitchOutlet.isOn, forKey: "sound")
        UserDefaults.standard.synchronize() 
    }
    
      //this is inside viewDidLoad and "boolValue" was declared outside viewDidLoad//
    
     boolValue = UserDefaults.standard.bool(forKey: "sound")
    

    【讨论】:

    • .synchronize() 没用。运行测试,你自己看看。
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多