Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:29:36+00:00 2026-05-22T02:29:36+00:00

Have UIPickerView with two rows. Trying to save value of each row in NSUserDefaults

  • 0

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];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:29:37+00:00Added an answer on May 22, 2026 at 2:29 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to handle two data sources in the UIPickerView? I have here
I have a UIPickerView that has two components. The number of rows in component
I have the following code to create a UIPickerView: pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,
I have a UIPickerView. I'm customizing it's rows via it's delegate's viewForRow as follows:
I have a UIPickerView with two components. It works fine when the user scrolls
I have a UIPickerView with 2 components. Here is the arrays loading into it:
I have a question here where I asked how to get a value form
I have a UIPickerView as subview in a UITableViewController, which I want to slide
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.