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];
}
}
In your
viewDidLoad:Where pickerView1 is visible whereas pickerView2 is not.
And wherever you want to display pickerView2 whilst hiding pickerView1 – just reverse the above.
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:
UPDATE 2:
Try this:
In your header (.h) file add this:
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:
And in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathreplace:with
And replace:
with:
Just remember to release them in your dealloc method:
Your undeclared problem should be solved now.