Есть 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];
}
}