ピッカー ビューからオブジェクトを選択して 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