简体   繁体   English

在UserDefaults中保存UISwitch状态

[英]Save UISwitch state in UserDefaults

I would like to save the state of a UISwitch label with UserDefaults. 我想用UserDefaults保存UISwitch标签的​​状态。 My code looks like this: 我的代码看起来像这样:

func viewDidAppear() {
    mySwitch.setOn(userDefaults.standard.bool(forKey: "mySwitchValue"), animated: true)
}

func viewWillDesappear() {
    UserDefaults.standard.set(mySwitch.isOn, forKey: "mySwitchValue")
}

But in the app, when I leave the switch view, and I return in, the UISwitch isn't as I turned it. 但是在应用程序中,当我离开交换机视图,然后我返回时,UISwitch并不像我转过它那样。

Probably, what rmaddy pointed out earlier is the issue. 可能,rmaddy之前指出的是问题。 In that case go for spell thingy. 在那种情况下,去拼写thingy。

Otherwise, it is possible that setting the value of your switch's state when view is disappearing is not a judicious choice. 否则,当视图消失时,设置开关状态的值可能不是一个明智的选择。 As when application goes into background other processes are acted upon alongside, and probably setting default is not effected before application closes. 当应用程序进入后台时,其他进程会同时执行,并且可能在应用程序关闭之前设置默认值。

I would generally set such values when I am calling such functions, ie, in the switch action. 我通常在调用这些函数时设置这样的值,即在switch操作中。 As soon as a user changes the switch state, save it in defaults , that way when you retrieve it when viewDidAppear , it will work. 一旦用户更改了开关状态,请将其保存为defaults ,这样当您在viewDidAppear检索它时,它将起作用。

import UIKit

class ViewController: UIViewController {

    let userDefaults = UserDefaults.standard

    @IBOutlet weak var mySwitch: UISwitch!

    @IBAction func switchAction(_ sender: UISwitch) {
        userDefaults.set(sender.isOn, forKey: "mySwitchValue")
    }

    override func viewDidAppear(_ animated: Bool) {
        mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
    }
}

Demo below: 演示如下:

在此输入图像描述

This is not an answer to your original query, but an answer to another query in the comment. 这不是原始查询的答案,而是对评论中另一个查询的答案。 Question : How to set the default state of UISwitch as on, if application is launched for the first time? 问题 :如果首次启动应用程序,如何将UISwitch的默认状态设置为on? Though ideally, it should be asked as another question, given it is incremental, the code is below: 虽然理想情况下,它应该被问为另一个问题,因为它是增量的,代码如下:

import UIKit

class ViewController: UIViewController {

    let userDefaults = UserDefaults.standard

    var firstTimeAppLaunch: Bool {
        get {
            // Will return false when the key is not set.
            return userDefaults.bool(forKey: "firstTimeAppLaunch")
        }
        set {}
    }

    @IBOutlet weak var mySwitch: UISwitch!

    @IBAction func switchAction(_ sender: UISwitch) {
        userDefaults.set(sender.isOn, forKey: "mySwitchValue")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if !firstTimeAppLaunch {
            // This will only be trigger first time the application is launched.
            userDefaults.set(true, forKey: "firstTimeAppLaunch")
            userDefaults.set(true, forKey: "mySwitchValue")
        }

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
    }

}

Note that you could do this within AppDelegate's function: 请注意,您可以在AppDelegate的功能中执行此操作:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Could add the above code within this as well. Upto you. 
        return true
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM