Cannot set NSUserDefault ToggleSwitch Value
I have a form within my IPad app that allows the user to configure the application settings (they can also cha开发者_Go百科nge these settings from the IPad settings app). I have the following code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:externalAddress forKey:@"firstString_preference"];
[defaults setValue:internalAddress forKey:@"secondString_perference"];
[defaults setValue:isConnectedToDemoString forKey:@"firstToggle_preference"];
[defaults setValue:isConnectedToInternal forKey:@"secondToggle_preference"];
[defaults synchronize];
The firstString_preference and secondString_perference are Textfields in the settings bundle and they save to the settings without issue. My problem is the firstToggle_preference and secondToggle_preference are toggle switches in the settings bundle and I cannot seem to set these at all. They always seem to be set to no.
Does anyone know what I am doing wrong? Should i be using a different method for setting Toggle Switch default values?
Thanks in advance
What type are isConnectedToDemoString
and isConnectedToInternal
. Unless those are NSNumber
s, you should use setBool:forKey:
.
Also, while setValue:forKey:
works for storing object types to the user defaults, your code would be clearer if you used the NSUserDefaults
-native setObject:forKey:
method.
精彩评论