I am making a utility application. On the flipside, I have a UISegmentedControl. Depending on the user's selection, a labe开发者_开发百科l on the main view will say one of two things. And while this functionality works, for whatever reason, whenever the user goes to the settings (flipside) of the app, the segmentedcontrol has the same part of the control highlighted. If the user presses part 0, then returns to the app, then goes back to the settings panel, the segmented control will show part 0 selected. If the user presses part 1, then returns to the app, then goes back to the settings panel, the segmented control will again show part 0 selected. I can disable this, by not choosing either as selected, but the problem still remains. When the user makes a selection, then goes to the main view, then returns to the settings pane, again nothing is selected. How can I retain the user's selecting, so that when they return to the settings pane, it will have highlighted his or her most recent selection?
NSUserDefaults ended up being the answer:
[[NSUserDefaults standardUserDefaults] setInteger:segmentedControl.selectedSegmentIndex
forKey:@"someKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
Later, if you want to retrieve it:
NSInteger selectedSegment = [[NSUserDefaults standardUserDefaults]
integerForKey:@"someKey"];
精彩评论