PDA

View Full Version : Change segmented selected index based off NSUserDefaults




newtoiphonesdk
Feb 11, 2012, 11:54 AM
I am using a segmented control in my settings view to make certain changes. Everything is working fine for when I change values on the control, but how do I get it to retain the index based off of the NSUserDefault?



forum user
Feb 11, 2012, 12:44 PM
Not sure I understand the question.
NSUserDefaults is a dictionary and does not like the zero form an integer (segmentIndex). If you make your int into an NSString it can be stored in the Dictionary with out problems even when zero. Get it back with intValue.

- Olaf

newtoiphonesdk
Feb 11, 2012, 01:01 PM
Not sure I understand the question.
NSUserDefaults is a dictionary and does not like the zero form an integer (segmentIndex). If you make your int into an NSString it can be stored in the Dictionary with out problems even when zero. Get it back with intValue.

- Olaf

Finally got it working. Problem I had originally was I set the int in wrong order for segmentedcontrol. Here is what I ended with:
-(void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSURL *sermons = [defaults URLForKey:@"sermon"];
NSString *absolutely = sermons.absoluteString;
NSLog(@"%@", absolutely);
if ([absolutely isEqualToString:@"http://www.bellavenue.org/sermons.htm"]) {
segment.selectedSegmentIndex = 1;
}
if ([absolutely isEqualToString:@"http://www.316apps.com/iphonesermons.html"]) {
segment.selectedSegmentIndex = 0;
}

}

dejo
Feb 11, 2012, 01:17 PM
Not sure I understand the question.
NSUserDefaults is a dictionary and does not like the zero form an integer (segmentIndex). If you make your int into an NSString it can be stored in the Dictionary with out problems even when zero. Get it back with intValue.

I would recommend using NSNumber rather than NSString to store ints in NSUserDefaults.

forum user
Feb 11, 2012, 01:45 PM
Ah, yes, true. NSNumber would keep it in the family.

- Olaf

newtoiphonesdk
Feb 11, 2012, 03:29 PM
Ah, yes, true. NSNumber would keep it in the family.

- Olaf
I didn't think this was storing any int, just the URL of websites.