2011-08-07 50 views
1

我以編程方式創建了2個UIPickerView。 pickerView2即將顯示時,如何確保隱藏pickerView1。反之亦然。1 ViewController上的多個UIPickerView

謝謝。

這就是我創建它們的方式。

- (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

在你viewDidLoad

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

哪裏pickerView1是而pickerView2可見不是。

無論你想在隱藏pickerView1的時候顯示pickerView2 - 只需改變上面的內容即可。

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

還有其他方法可以做到這一點,但這是我的首選方法。

UPDATE:

這裏是你將如何使用您的代碼實現:

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

你未申報的問題現在應該解決了。

+0

@MaTaKazer - 嘗試使用if語句更新我的答案。 –

+0

它獲取pickerView1&pickerView2未聲明,因爲它沒有被聲明時,它第一次進入if條件。 –

+0

@MaTaKazer'如果(pickerView1/2 == nil)'應該捕獲它,如果它是未申報的。您的原始代碼是否存在未聲明的問題? –

1
[self.view addSubview:pickerView1]; 

[pickerView2 removeFromSuperview]; 

//or 

[pickerView2 setAlpha:0.0]; 
+0

所有的答案看起來不錯,但我得到pickerView2未申報。它是如何創建我的pickerView? –

+0

在頭文件中爲pickerView2創建一個實例變量。 –

2

在安裝過程中:

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

當切換(這將在兩個方向上工作):

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

乾杯,
的Sascha

相關問題