我試圖學習如何從一個選擇器視圖中選擇一個對象以顯示在一個TextBox中,六個小時之後我以爲id最終破解了但沒有。它的工作原理,但我只能讓我的NSArray中的第一個項目顯示在我的文本框中。Picker View只在文本框中選擇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
IM也得到黃色警告三角s local decalration of "select" hides instance variable
您是否嘗試過記錄selectedRow(在您的IBAction中)以查看它是否總是返回0,無論您在選取器視圖中選擇哪一行?您是否仍然收到本地聲明錯誤,因爲我沒有看到您發佈的代碼中的任何內容會導致該錯誤? – rdelmar 2012-07-30 03:45:28
現在沒有本地聲明錯誤。將嘗試記錄並回報。 – JSA986 2012-07-30 09:14:35
排序實例變量錯誤導致了問題。感謝您幫助確認接受的答案 – JSA986 2012-07-30 10:04:48