无法在NSUserDefaults中保存UIPickerView值

时间:2011-05-15 16:07:41

标签: iphone uipickerview nsuserdefaults

UIPickerView有两行。试图在NSUserDefaults中保存每一行的价值。在NSUserDefaults之前没用过。这是我的代码:

-(void)viewWillAppear: (BOOL) animated {
    NSUserDefaults *defaultsPicker = [NSUserDefaults standardUserDefaults];
    picker = [defaultsPicker objectForKey:@"picker"] ;
}

- (void)viewDidLoad {

    NSArray *ar1 = [NSArray array];
    for (int i = 150; i <= 210; i++)
        ar1 = [ar1 arrayByAddingObject:[NSString stringWithFormat:@"%d",i]];
    [ar1 retain];
    self.array1 = ar1;

    NSArray *ar2 = [NSArray array];
    for (int j = 40; j <= 130; j++)
        ar2 = [ar2 arrayByAddingObject:[NSString stringWithFormat:@"%d",j]];
    [ar2 retain];
    self.array2 = ar2;


    [super viewDidLoad];

}

NSUserDefaults在IBAction中

-(IBAction)calculate {
    NSUserDefaults *defaultsPicker = [NSUserDefaults standardUserDefaults];
    [defaultsPicker setObject:[NSNumber numberWithInt:[picker selectedRowInComponent:0 ]] forKey:@"picker"];
    [defaultsPicker synchronize];

    //bla bla
}

当我按下按钮时,应用程序崩溃并且记录为:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber selectedRowInComponent:]: unrecognized selector sent to instance 0x690df40'

更新

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
    if (component == secondArrayComponent) {
        return [self.array2 count];
    }else {
        return [self.array1 count];
    }
}


#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == secondArrayComponent) {
        return [self.array2 objectAtIndex:row];
    }else {
        return [self.array1 objectAtIndex:row];
    }
}

1 个答案:

答案 0 :(得分:2)

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 委托方法中添加以下代码,它应保存到NSUserDefaults

<强>更新

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
    if (component == secondArrayComponent) {
        return [self.array2 count];
    }else {
        return [self.array1 count];
    }
}


#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == secondArrayComponent) {
        return [self.array2 objectAtIndex:row];
    }else {
        return [self.array1 objectAtIndex:row];
    }
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {    
    NSInteger selectedRow = [thePickerView selectedRowInComponent:0];
    [[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:@"picker"];
}

只要您确保在界面构建器或代码中将UIPickerView的委托设置为“self”,就应该这样做。同样在您的.h文件中,请确保您有<UIPickerViewDelegate>

更新2 要检查您的NSUserDefaults是否已保存,请在您选择的 - (IBAction)中使用此代码

NSInteger pickerRow = [[NSUserDefaults standardUserDefaults] integerForKey:@"picker"];
NSLog(@"%i", pickerRow);