簡體   English   中英

使用UserDefaults保存和檢索布爾值

[英]Saving and retrieving a bool with UserDefaults

我正在嘗試從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//

由於某種原因,這段代碼給了我錯誤,而我嘗試過的所有事情都沒有奏效。 如何將布爾值保存到UserDefaults並在另一個視圖中檢索它?

編輯:我想我固定了第一部分。 但是,我檢索布爾值的方式似乎是完全錯誤的。 另外:沒有其他stackExchange答案可以響應我的要求,至少不能迅速做出響應。

正如Leo在評論中提到的那樣, bool(forKey返回一個非可選的Bool 。如果鍵不存在,則返回false

所以這很簡單

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

不需要按照其他答案中的建議調用synchronize() 框架會定期更新用戶默認數據庫。

像這樣做。

在您的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.


    }

}

第一個視圖控制器的結尾

現在在第二個視圖控制器中

  • 您可以獲得在first view controller設置的userdefault的值。 我將其放在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()

        }




    }

希望這對您有幫助。 祝好運

使用以下代碼行:

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

代替 :

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

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM