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];
}
Thanks!
I fail to understand what your real issue is. Your code should work given that you only spin component 0 and not 1. There’s an issue with storing the row for two components into one setting. If you spin component 1 to row 20 and then load the view it will most likely crash unless component 0 also has 20 rows.
Please provide an error message or something that indicate what is wrong. Also make sure that your viewWillAppear method is actually called.