Picker View仅在文本框中选择NSArray中的第一个对象

时间:2012-07-29 19:55:11

标签: objective-c ios5 textbox xcode4.2 uipickerview

我试图学习如何从一个选择器视图中选择一个对象来显示在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

1 个答案:

答案 0 :(得分:1)

对于黄色警告,请更改.m文件中的以下代码行。

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

以上替换为:

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

因为你已经在.h文件中声明了uipicker * select。