How to store UISegmentedControle state in NSUserdefaults
The problem is when de selectedSegmentIndex is unselected: "UISegmentedControlNoSegment" alias "-1".
The other states (0, 1, 2 , etc.), I can store as Integers and retrieve with
carTypeSegmentedControl.selectedSegmentIndex = [defaults integerForKey:@"typeOfCar"];
But -1 is no NSInteger.
I also tried to remove the Integer out of the NSUserdefaults but a request would return an "0", which is not acceptable.
So, is t开发者_StackOverflow社区here another easy way?
-1 is an integer, it's a negative integer. You should be able to store this in NSUserDefaults.
You need to use NSNumber
, which is an object representation of values of common numerical and boolean types, e.g. to set:
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:-1] forKey:kDefaultCarSegmentedControlState];
To retrieve:
NSInteger defaultCarSegmentedControlSelectedIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:kDefaultCarSegmentedControlState] intValue];
You are right Ben, -1 is indead an integer. And it is stored after all. I did an NSLog on that. Stupid i didn't try that. I didn't find clear documentation on that.
So probably my problem is somewhere els.
I guess your answer works as well, Alex.
Thank you for helping
精彩评论