PDA

View Full Version : UIPickerView and NSUserDefaults issue




guydor
Aug 8, 2010, 11:10 AM
Hi,

I have an UIPickerView with 2 components. I would like to save the user's selection and to apply it next time the pickerView is shown again.

Here's my code:


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
[pickerViewSelectionDefaults setInteger:row forKey:@"pickerViewSelectionKey"];
[pickerViewSelectionDefaults synchronize];

}

}


And...


- (void)viewWillAppear:(BOOL)animated {

NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
[pickerView selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewSelectionKey"] inComponent:0 animated:YES];

}


The problem is that this NSUserDefaults settings don't save the UIPickerView selections.
My pickerView has 2 components.

Thanks