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

  • SEARCH
  • Home
  • 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 5932153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:47:43+00:00 2026-05-22T14:47:43+00:00

I am trying to display a single UIPickerView in my app and depending on

  • 0

I am trying to display a single UIPickerView in my app and depending on what button is selected a UIPickView will popup and populate with the relevant data for the button selected. No sure if it matters but one UIPickerView has three columns and one has two. I can get both of them to work perfectly when the are separate but the minute that I nest them into if statements it breaks.

On the third time when I select the button that fires the opposite UIPickerView it crashes and give me the error of NSRangeException and that it is trying to find the object at index 2 in an array [0…1]. My understanding is that the program thinks that there is an object in a spot in the array that doesn’t exist.

I have tried the [picker reloadAllComponents] method and have it called each time the button is selected but as stated above that only works once and then it quits. The strange thing is that if I select the button that I pressed for the second set of code it works perfectly. It is when I select a button that would make the UIPickerView have to change its view that it crashes.

I am really new to iOS development and Objective C and hope that someone out there knows what I am doing wrong. I will post any code that you guys need and will post a couple samples just to understand what is going on.

Here is one of the buttons methods that sets an int to 2 and also calls the method to show the picker.

-(IBAction)linePopupPicker{
picker = 2;
[self pickerShow];
}

This code is the other method which is pretty much the same as the other (I am aware that I haven’t implemented the UserDefaults into the second button yet)

-(IBAction)namePicker{
field  = 1;
picker = 1;
NSLog(@"The value of field is:%i", field);
NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
[tipPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewSelectionKey"] inComponent:0 animated:NO];
[tipPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewNameColor"] inComponent:1 animated:NO];
[tipPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewNameFontSize"] inComponent:2 animated:NO];

[self pickerShow];
}

This is the method that calls the UIPickerView. I have it so that it emerges onto the view rather than being static. I thought that this would be the best place to reload the components.

-(IBAction)pickerShow{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 240);
pickerView.transform = transform;
[self.view addSubview:pickerView];


[tipPicker reloadAllComponents];


[UIView commitAnimations];  
}

Finally here is just a sample of the code that is used throughout the various methods that need to be called for the UIPickerView.

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (picker ==2) {
    switch (component)
    {
        case 0:
        {
            UILabel *lineColorLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 44)] autorelease];
            lineColorLabel.backgroundColor = [UIColor clearColor];
            NSString *lineTempColor = [colorArray1 objectAtIndex:row]; 
            //NSLog(@"The String Color Name:%@", tempColor);
            UIColor *myColor1 = [UIColor performSelector:NSSelectorFromString(lineTempColor)];
            lineColorLabel.backgroundColor =  myColor1;
            return lineColorLabel;
        }
        case 1:
        {
            UILabel *alphaLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0,40, 44)] autorelease];
            alphaLabel.text = [alphaArray objectAtIndex:row];
            alphaLabel.font = [UIFont systemFontOfSize:18];
            alphaLabel.textAlignment = UITextAlignmentCenter;
            alphaLabel.backgroundColor = [UIColor clearColor];
            alphaLabel.shadowColor = [UIColor whiteColor];
            return alphaLabel;
        }
        default:
            break;
    }
}

else if (picker == 1) 
 {
    switch (component)
    {
        case 0:
        {
            UILabel *fontLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 44)] autorelease];
            fontLabel.backgroundColor = [UIColor clearColor];
            fontLabel.shadowColor = [UIColor whiteColor];
            fontLabel.text = [fontNames objectAtIndex:row];
            NSString *font = [fontNames objectAtIndex:row];
            fontLabel.font = [UIFont fontWithName:font size:18.0];
            fontLabel.textAlignment = UITextAlignmentLeft;
            return fontLabel;
        }
        case 1:
        {


            UILabel *colorLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 44)] autorelease];
            NSString *tempColor = [colorArray objectAtIndex:row]; 
            //NSLog(@"The String Color Name:%@", tempColor);
            UIColor *myColor = [UIColor performSelector:NSSelectorFromString(tempColor)];
            colorLabel.backgroundColor =  myColor;


            return colorLabel;
        }
        case 2:
        {
            UILabel *sizeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0,40, 44)] autorelease];
            sizeLabel.text = [fontSizeArray objectAtIndex:row];
            sizeLabel.font = [UIFont systemFontOfSize:18];
            sizeLabel.textAlignment = UITextAlignmentCenter;
            sizeLabel.backgroundColor = [UIColor clearColor];
            sizeLabel.shadowColor = [UIColor whiteColor];
            return sizeLabel;
        }
        default:
            break;
    }

}

    return 0;
}

Here are the data methods:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 
if (picker == 2) {
    return 2;
}else if (picker == 1) {
    return 3;
}

//return 3; 
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have.
if (picker == 2) {
    if (component == 0) {
        return [colorArray1 count];
    }
    return [alphaArray count];
}else if (picker == 1) {
    if (component == 0) {
        return [fontNames count];
    }else if (component == 1) {
        return [colorArray count];
    }
    return [fontSizeArray count];
}

}

I hope this post provides all of the information that is need to help me get this fixed. I really appreciate it and hope that I am just missing something really simple. Thank you in advanced.

I am really new to iOS development and Objective C so this problem is really stumping me.

  • 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-22T14:47:43+00:00Added an answer on May 22, 2026 at 2:47 pm

    case 1 in the if ( picker == 2) switch statement has an opening brace but no closing one.

    EDIT

    - (void) savePickerOne {
        [pickerViewSelectionDefaults setInteger:[tipPicker selectedRowInComponent:0] ForKey:@"pickerViewSelectionKey"];
        [pickerViewSelectionDefaults setInteger:[tipPicker selectedRowInComponent:1] ForKey:@"pickerViewNameColor"];
        [pickerViewSelectionDefaults setInteger:[tipPicker selectedRowInComponent:2] ForKey:@"pickerViewNameFontSize"];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to have a label display single-digit numbers as double-digit numbers (ie.
I am trying to display 10 html pages as a single document,with 10 chapters.
I am trying to display a single item (not contained in a collection) using
I'm trying to display multiple, static jQuery Progress Bars on a single page, all
I am trying to display a tabular set of data in a databound control,
I'm trying to load a single JLabel set with an ImageIcon for display. It
I've trying to build a user control that will display the content of a
I am trying to display a 'Software Release' table in an asp.net dynamic data
I'm trying to write a program to display PCM data. I've been very frustrated
I am trying to display a table (or ul) that will contain a navigation

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.