1个ViewController上的多个UIPickerView

时间:2011-08-07 09:05:15

标签: iphone xcode uipickerview

我以编程方式创建了2个UIPickerView。当pickerView2即将显示时,如何确保隐藏pickerView1。反之亦然。

谢谢。

这就是我创建它们的方式。

  - (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];
        }
    }

3 个答案:

答案 0 :(得分:2)

在设置过程中:

pickerView1.hidden = NO;
pickerView2.hidden = YES;

切换时(这将在两个方向都有效):

pickerView1.hidden = !pickerView1.hidden;
pickerView2.hidden = !pickerView1.hidden;

干杯,
的Sascha

答案 1 :(得分:1)

[self.view addSubview:pickerView1];

[pickerView2 removeFromSuperview];

//or

[pickerView2 setAlpha:0.0];

答案 2 :(得分:1)

viewDidLoad

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

其中pickerView1可见,而pickerView2则不可用。

无论你想在隐藏pickerView1的同时显示pickerView2 - 只需颠倒上面的内容。

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

还有其他方法可以做到这一点,但这是我的首选方式。

<强>更新

以下是使用代码实现它的方法:

 - (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];
    }
}

更新2:

试试这个:

在标题(.h)文件中添加以下内容:

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

您必须在此处声明您的pickerViews才能使我之前的代码正常工作。另请注意<UIPickerViewDelegate, UIPickerViewDataSource>协议。

回到您的实现(.m)文件中,记得合成:

@synthesize pickerView1;
@synthesize pickerView2;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath替换:

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

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

并替换:

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

使用:

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

请记住在你的dealloc方法中释放它们:

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

你的未申报问题现在应该解决了。