Can't save UIPickerView value in NSUserDefaults
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];
pick开发者_StackOverflow中文版er = [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];
}
}
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);
精彩评论