How Can I Save A Boolean In NSDefaults When A Button Is Pressed?

北战南征 提交于 2019-12-24 16:19:25

问题


I've coded a night and day button theme. If the user presses the day button, GlobalData.DayBool = true, and GlobalData.NightBool = false. When they press the night button, GlobalData.DayBool = false and GlobalData.NightBool = true. The background changes to night or white, depending on which one is true and false.

Let's say that the user clicks the Night Button. GlobalData.NightBool = true, and GlobalData.DayBool = false, and the background changes to black.

I want the booleans to be saved depending on which button they press, so that when they close the app, their background will be the same as they had selected.

//Day Button Pressed
for touch: AnyObject in touches {

    let location = touch.locationInNode(self)

    if dayButton.containsPoint(location) {

        for touch: AnyObject in touches {
            _ = touch.locationInNode(self)

            backgroundColor = GlobalData.dayColor
            GlobalData.nightBool = false
            GlobalData.dayBool = true
        }
    }
}

//Night Button Pressed
for touch: AnyObject in touches 
    let location = touch.locationInNode(self)

    if nightButton.containsPoint(location) {

        for touch: AnyObject in touches {
            _ = touch.locationInNode(self)

            backgroundColor = GlobalData.nightColor
            GlobalData.nightBool = true
            GlobalData.dayBool = false 
        }
    }
}

Here's my code for when it switches backgrounds depending whether one of them is true or false:

//Day Background
if GlobalData.dayBool == true && GlobalData.nightBool == false {
    backgroundColor = GlobalData.dayColor
}

//Night Background
if GlobalData.nightBool == true && GlobalData.dayBool == false {
    backgroundColor = GlobalData.nightColor
}

Please leave any questions below if you have any. I really need help on this.


回答1:


Create an IBAction for your button and do the NSUserDefault section below.

NSUserDefault

I always like to register my NSUserDeafult to default setting, a lot of people just continue with the second step without registering.

Register NSUserDefault in AppDelgate.swift

NSUserDefaults.standardUserDefaults().registerDefaults(["valueName": AnyObject])

Set Value to your NSUserDefault, this depends on what type of data you're storing, should match the one with your registration if you did register. (Example of Boolean data type below)

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "valueName") //Bool Data Type

Make sure you synchronize once you set the value to the NSUserDefault, this way it will update instantly, otherwise it will update when it get a chance.

NSUserDefaults.standardUserDefaults().synchronize()

Receive Value: this will receive boolean value since we set boolean and register boolean.

let Variable: Bool! = NSUserDefaults.standardUserDefaults().boolForKey("valueName")



回答2:


In AppDelegate / applicationDidFinishLaunching register the key/value pairs, assuming day is true by default and night is false.

let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["day" : true, "night" : false]
defaults.registerDefaults(defaultValues)

Then read the values wherever you want (but always after registering them):

let defaults = NSUserDefaults.standardUserDefaults()
GlobalData.dayBool = defaults.boolForKey("day")
GlobalData.nightBool = defaults.boolForKey("night")

Consider that there are no optionals at all!

Save the values as well wherever you like (preferable in applicationWillTerminate):

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(GlobalData.dayBool, forKey("day")
defaults.setBool(GlobalData.nightBool, forKey("night")
defaults.synchronize()

The synchronize line can be omitted.

As day seems to be always the opposite of night, one default key might be sufficient.



来源:https://stackoverflow.com/questions/34373668/how-can-i-save-a-boolean-in-nsdefaults-when-a-button-is-pressed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!