How can I save a second row of UIPickerView to NSUserDefaults?

本秂侑毒 提交于 2019-12-06 16:35:17

Try this:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
             inComponent:(NSInteger)component {    
    NSInteger selectedRow = [thePickerView selectedRowInComponent:component];
    NSString *key = [NSString stringWithFormat:@"picker%d", component];

    [[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:key];
}

and this

-(void)viewWillAppear: (BOOL) animated {
    NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
    [picker selectRow:[pickerViewSelectionDefaults integerForKey:@"picker0"] 
        inComponent:0 animated:YES];
    [picker selectRow:[pickerViewSelectionDefaults integerForKey:@"picker1"] 
        inComponent:1 animated:YES];
}

Add them with separate keys.

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:   (NSInteger)component {    
NSInteger selectedRow = [thePickerView selectedRowInComponent:0];

[[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:@"pickerRow1"];
[[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:@"pickerRow2"];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!