2017-02-27 56 views
-2

我试图从UISwitch中为UserDefaults保存一个bool值,并在另一个视图中检索它。但是,我已经尝试了以下多个教程和堆栈答案,而且没有一个可以工作。用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答案回应我的要求,至少不是很快。

+0

的可能的复制http://stackoverflow.com/questions/34373668/how-can-i-save-a-boolean-in-nsdefaults-when-a按钮被按下? – 2017-02-27 05:06:00

+0

'UserDefaults.standard.set(sender.isOn,forKey:“sound”)' –

+0

reading bool for key returns a Bool(not optional)'boolValue = UserDefaults.standard.bool(forKey:“sound”)' –

回答

5

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

所以它只是

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

调用synchronize()在其他的答案建议是没有必要的。该框架定期更新用户默认数据库。

+0

Thanks!交换机的状态保持保存状态,但是当我尝试在另一个视图上使用bool值时,它会卡住它开始的布尔值。 – Gabe12

+1

请勿滥用'UserDefaults'作为临时存储或在控制器之间传递数据。 – vadian

0

使用此行代码:

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

与其使用:

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

这样做。

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


    } 

} 

第一视图控制器

现在,在你的第二个视图控制器结束

  • 可以GE t您在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() 

     } 




    } 

希望这会帮助你。好运

+1

yep @EricAya我们不再需要它从iOS 8中。接受,我会删除它。 thanx –

-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") 
+0

'.synchronize()'没用。运行测试,你会看到自己。 – Moritz