使用UserDefaults保存和检索bool

时间:2017-02-27 05:02:31

标签: ios swift nsuserdefaults

我尝试将一个 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回答回应我所问的问题,至少不是快速回答。

4 个答案:

答案 0 :(得分:13)

正如评论中提到的Leo bool(forKey会返回一个非可选的Bool。如果密钥不存在,则返回false

所以它只是

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

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

答案 1 :(得分:2)

这样做。

first view controller

  • IBoutlet

  • 建立UISwitch关联
  • 然后是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()

        }




    }

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

答案 2 :(得分:1)

使用以下代码行:

gl.drawXXX

代替:

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

答案 3 :(得分:-1)

试试这个:

@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")