0

我试图学习如何从选择器视图中选择一个对象以显示在一个 TextBox 中,我认为 id 终于破解了六个小时,但没有。它有效,但我只能让我的 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 回答