无法在 NSUserDefaults 中保存 UIPickerView 值

发布于 2024-11-07 08:36:27 字数 2076 浏览 0 评论 0原文

有两行的 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];
    }
}

Have UIPickerView with two rows. Trying to save value of each row in NSUserDefaults. Didn't used before NSUserDefaults. Here is my code:

-(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];

}

And NSUserDefaults is in IBAction

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

    //bla bla
}

When I press the button, app crashes and log is:

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

UPDATE

#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];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-11-14 08:36:27

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

UPDATE

#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 文件中,确保您有

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

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

In your - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
delegate method add the following code and it should save to NSUserDefaults

UPDATE

#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"];
}

That should do as long as you make sure you have set your UIPickerView's delegate as "self" in interface builder or in your code. Also in your .h file make sure you have <UIPickerViewDelegate>

UPDATE 2
To check whether your NSUserDefaults have saved use this piece of code in an -(IBAction) of your choosing

NSInteger pickerRow = [[NSUserDefaults standardUserDefaults] integerForKey:@"picker"];
NSLog(@"%i", pickerRow);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文