在你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];
}
你未申報的問題現在應該解決了。
@MaTaKazer - 嘗試使用if語句更新我的答案。 –
它獲取pickerView1&pickerView2未聲明,因爲它沒有被聲明時,它第一次進入if條件。 –
@MaTaKazer'如果(pickerView1/2 == nil)'應該捕獲它,如果它是未申報的。您的原始代碼是否存在未聲明的問題? –