2017-02-27 55 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