无法在 NSUserDefaults 中保存 UIPickerView 值
有两行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
委托方法添加以下代码,它应该保存到 NSUserDefaultsUPDATE
只要您确保已在界面生成器或代码中将 UIPickerView 的委托设置为“self”,就应该这样做。另外,在您的 .h 文件中,确保您有
UPDATE 2
要检查您的 NSUserDefaults 是否已保存,请在您选择的 -(IBAction) 中使用这段代码
In your
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
delegate method add the following code and it should save to NSUserDefaultsUPDATE
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