0

ピッカー ビューからオブジェクトを選択して TextBox に表示する方法を学ぼうとしています。動作しますが、NSArray の最初の項目しか取得できず、テキスト ボックスに表示されません。

Select私のピッカーでarrstatusあり、私の NSArray でtext1あり、私のテキスト ボックスです。

ボタンを使用して値を取得: IBAction の下:

NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex:selectedRow];
  [text1 resignFirstResponder];

コードの残りの部分:

.h

@interface pick5 : UIViewController <UIPickerViewDataSource,     UIPickerViewDelegate,UITextFieldDelegate>

 {

UIPickerView *select;

NSArray *arrStatus;


}

.m

UIPickerView *select = [[UIPickerView alloc] initWithFrame:CGRectZero];
select.delegate = self;
select.dataSource = self;
[select setShowsSelectionIndicator:YES];
text1.inputView = select;

arrStatus = [[NSArray alloc] initWithObjects:@"bs88", @"bs60898", @"Memphis",      @"California", @"Seattle",@"London",@"Paris", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
  return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 {
//set number of rows
return arrStatus.count;
  }

 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row   forComponent:(NSInteger)component
 {
//set item per row
return [arrStatus objectAtIndex:row];













[super viewDidLoad];
// Do any additional setup after loading the view.
 }

 - (void)viewDidUnload
 {
[self setText1:nil];
[self setResign:nil];
[self setText2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

 - (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}





 - (IBAction)resign:(id)sender {


NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];
}
@end

私も黄色の警告三角形を取得していますlocal decalration of "select" hides instance variable

4

1 に答える 1

1

黄色の警告については、.m ファイルの次のコード行を変更してください。

UIPickerView *select = [[UIPickerView alloc] initWithFrame:CGRectZero];

上記を以下に置き換えます。

select = [[UIPickerView alloc] initWithFrame:CGRectZero];

.h ファイルで既に uipicker* select を宣言しているためです。

于 2012-07-30T06:46:09.780 に答える