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 6246309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:39:53+00:00 2026-05-24T12:39:53+00:00

I created 2 UIPickerView Programatically. How do I make sure to hide pickerView1 when

  • 0

I created 2 UIPickerView Programatically. How do I make sure to hide pickerView1 when pickerView2 is about to show. Vice versa.

Thank you.

This is how I created them.

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1){
            [scrollView setContentOffset:CGPointMake(0, 223) animated:YES];
            CGRect pickerFrame = CGRectMake(0, 152, 0, 0);

            UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:pickerFrame];
            pickerView1.showsSelectionIndicator = YES;
            pickerView1.dataSource = self;
            pickerView1.delegate = self;
            [pickerView1 setTag:1];
            [self.view addSubview:pickerView1];
            [pickerView1 release];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

        }
        else if (indexPath.row == 2){
            [scrollView setContentOffset:CGPointMake(0, 273) animated:YES];
            CGRect pickerFrame = CGRectMake(0, 152, 0, 0);

            UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:pickerFrame];
            pickerView2.showsSelectionIndicator = YES;
            pickerView2.dataSource = self;
            pickerView2.delegate = self;
            [pickerView2 setTag:2];
            [self.view addSubview:pickerView2];
            [pickerView2 release];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
  • 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-24T12:39:53+00:00Added an answer on May 24, 2026 at 12:39 pm

    In your viewDidLoad:

    [pickerView1 setAlpha:1.0];
    [pickerView2 setAlpha:0.0];
    

    Where pickerView1 is visible whereas pickerView2 is not.

    And wherever you want to display pickerView2 whilst hiding pickerView1 – just reverse the above.

    [pickerView1 setAlpha:0.0];
    [pickerView2 setAlpha:1.0];
    

    There are other ways of doing this but this is my preferred way of doing it.

    UPDATE:

    Here’s how you would implement it using your code:

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 1){
            [scrollView setContentOffset:CGPointMake(0, 223) animated:YES];
            CGRect pickerFrame = CGRectMake(0, 152, 0, 0);
    
            if (pickerView1 == nil) { //<--- You don't need to keep calling alloc and init. This if statement only calls if you have not already declared pickerView1.
                UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:pickerFrame];
                pickerView1.showsSelectionIndicator = YES;
                pickerView1.dataSource = self;
                pickerView1.delegate = self;
                [pickerView1 setTag:1];
                [self.view addSubview:pickerView1];
            }
    
            [pickerView1 setAlpha:1.0]; //<--- New Code
            if (pickerView2 != nil) { //<-- Checking if pickerView2 is declared yet, if not then it is already invisible. ;)
                [pickerView2 setAlpha:0.0];
            }
    
            //removed release call - add to dealloc method
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        }
        else if (indexPath.row == 2){
            [scrollView setContentOffset:CGPointMake(0, 273) animated:YES];
            CGRect pickerFrame = CGRectMake(0, 152, 0, 0);
    
            if (pickerView2 == nil) { //<-- again, no need to always alloc and init
                UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:pickerFrame];
                pickerView2.showsSelectionIndicator = YES;
                pickerView2.dataSource = self;
                pickerView2.delegate = self;
                [pickerView2 setTag:2];
                [self.view addSubview:pickerView2];
            }
    
            [pickerView2 setAlpha:1.0]; //<--- New Code
            if (pickerView1 != nil) { //<-- Checking if pickerView1 is declared yet, if not then it is already invisible. ;)
                [pickerView1 setAlpha:0.0];
            }
            //add release call to dealloc method
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
    

    UPDATE 2:

    Try this:

    In your header (.h) file add this:

        @interface <name>ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
            UIPickerView *pickerView1;
            UIPickerView *pickerView2;
        }
        @property (nonatomic, retain) UIPickerView *pickerView1;
        @property (nonatomic, retain) UIPickerView *pickerView2;
        @end
    

    You have to declare your pickerViews here for my previous code to work. Also note the <UIPickerViewDelegate, UIPickerViewDataSource> protocols.

    Back in your implementation (.m) file, remember to synthesize:

    @synthesize pickerView1;
    @synthesize pickerView2;
    

    And in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath replace:

    UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:pickerFrame];
    

    with

    pickerView1 = [[UIPickerView alloc] initWithFrame:pickerFrame];
    

    And replace:

    UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:pickerFrame];
    

    with:

    pickerView2 = [[UIPickerView alloc] initWithFrame:pickerFrame];
    

    Just remember to release them in your dealloc method:

        - (void)dealloc {
            [pickerView1 release];
            [pickerView2 release];
            [super dealloc];
        }
    

    Your undeclared problem should be solved now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a subclass of UIPickerView. In this subclass I've added a UIToolbar as
I created a UIPickerView inside an UIActionSheet , using the following tutorial: Add UIPickerView
I've created a UIPickerView, which displays on button click. The question is how I
I created a view with a UIPickerView which is showed when user push a
I have created a UITextField along with UIPickerView programmatically and set the input view
How would I go about adding gesture events to uipickerview to change tabs? I
I created the HelloTabWidget tutorial app for android and cannot seem to make it
I have created two view controllers in XCode along with a UIPickerView, a label,
I found Horizontal UIPickerView . but this code is not working . (//this: )is
I have the following code to create a UIPickerView: pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,

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.