ios - Problems with storing UISwitch state -
I have been having trouble keeping a setting in my app that made me status of I in fact, UISwitch default is turned on, but when my app is updated to a new version, it is stopped I found out that if the user has opened the Settings menu in version 1.0, so it is no problem, but if They have never changed the setting, or set The eing menu is also opened, so this is a problem. Therefore, this one issue should be somewhere in the viewWillAppear () , but I do not know what it is. The code I switch is for state storage really looks overcomplicated
This is the code I have in my settings menu.
@IBAction function dupOffOnSwitch (this: AnyObject) {if dupSwitch.on == true {autoAdjust = true println (autoAdjust)} else {autoAdjust = false println (autoAdjust)} NSUserDefaults.standardUserDefaults ( ) setBool (autoAdjust, forKey: "autoAdjustSettings").} viewWillAppear function overrides (animated: bool) {autoAdjust = NSUserDefaults.standardUserDefaults (). boolForKey ( "autoAdjustSettings") If autoAdjust == true {dupSwitch.on = true} else {dupSwitch.on = false}} if userReturnedAuto == false {dupSwitch.on = true themeSwitch .on = false userReturnedAuto = true NSUserDefaults.standardUserDefaults ( ) SetBool (userReturnedAuto, forKey: "userReturnedAuto") .. NSUserDefaults.standardUserDefaults () setBool (userReturnedAuto, forKey: "au ToAdjustSettings")} I'm declared bool 'autoAdjust' as a global variable in a different view controller.
This code is in the same view controller that is in this code:autoAdjust = NSUserDefaults.standardUserDefaults () boolForKey ("autoAdjustSettings")<:viewWillAppear ()is. / Pre>Whether the app is being launched for the first time, whenever a switch on the default, a switch can recommend a better way to store the state? Or a fix to my current solution.
NSUserDefaults.standardUserDefaults (). BoolForKey ("key") default to false, in your case, if the user never ever sets the key, it will be wrong and the switch will be off
Instead, the key Rename and use it as a negative. Instead, call it autoAdjustSettingsOff which will be the default default and the switch will be the default on the ON. Do not forget to switch around the right / wrong settings in your conditional block.
Comments
Post a Comment