I have a UISwitch, witch among other things, disables a UITextfield.
When using NSUserDefaults, I manage the switch to remember its current state (on/off)
NSString *const MyPrefKey =@"MyPrefKey";
[[NSUserDefaults standardUserDefaults]
setBool:[sender isOn]
forKey:MyPrefKey];
The switch state is recalled in viewDidLoad with
BOOL switchState =[[NSUserDefaults standardUserDefaults]boolForKey:MyPrefKey];
[mySwitch setOn:switchState];
This works perfect, the position of the UISwitch is remembered.
For enabling the textfield when the switch changed
I tried as suggested:
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithBool:[myTextField isEnabled]]
forKey:MyPrefKey];
and recalled with
BOOL mtf =[[NSUserDefaults standardUserDefaults]boolForKey:MyPrefKey];
[myTextField setEnabled:mtf];
Which also worked fine.
However, only one of these work at the time. How do I make more defaults to be set at the same time? Do I have to make several PrefKeys? or can all of them be stored in the same prefKey?
Finally I tried to change the textcolor with NSUserDefaults
[[NSUserDefaults standardUserDefaults]
setObject: [myTextField textColor]
forKey: MyPrefKey];
that causes a crash when recalled with
UIColor*newColor= [[NSUserDefaults standardUserDefaults] objectForKey: EfloraSharePrefKey];
[myTextField setTextColor:newColor];
So how do I use NSUserDefault with textColor? I would appriciate any good solutions.
You can save property list objects only to NSUserDefaults. You have to somehow serialize your color. A possible solution:
And to retrieve it:
If you want to be really elegant, you can wrap this into a category and even make UIColor NSCoding-comformant (in order to be able to directly persist it to the user defaults plist).