我已经使用2个数组在相应的Picker View中传递数据。我在UITextField
上通过了Picker Views。我在特定的文本字段选择上有2个文本字段,应该会出现相关的选择器视图。我试过一些方法仍然没有结果。
以下是代码:
//First Picker View
self.pickerFillStatus = [[UIPickerView alloc]init];
self.pickerFillStatus.showsSelectionIndicator = YES;
self.pickerFillStatus.delegate = self;
self.pickerFillStatus.dataSource =self;
self.fillArray = [NSArray arrayWithObjects:@"Single",@"Married Filling Jointly",@"Married Filling Separately",@"Head of House Hold",nil];
self.txtFillingStatus.inputView = self.pickerFillStatus;
self.txtFillingStatus.tag = 1;
// Second Picker View
self.pickerState = [[UIPickerView alloc]init];
self.pickerState.showsSelectionIndicator = YES;
self.pickerState.delegate = self;
self.pickerState.dataSource =self;
self.stateArray = [NSArray arrayWithObjects:@"SC",@"VT",@"OR",@"PA",@"RI",nil];
self.txtState.inputView = self.pickerState;
self.txtState.tag = 2;
// All the Datasource and Delegates
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
if (self.txtFillingStatus.tag == 1) {
return self.fillArray.count;
} else if (self.txtState.tag == 2) {
return self.stateArray.count;
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
if (self.txtFillingStatus.tag == 1) {
[_txtFillingStatus setText:[self.fillArray objectAtIndex:row]];
}else if (self.txtState.tag == 2) {
[_txtState setText:[self.stateArray objectAtIndex:row]];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)row forComponent:(NSInteger)component
{
if (self.txtFillingStatus.tag == 1) {
return [self.fillArray objectAtIndex:row];
}else if (self.txtState.tag == 2) {
return [self.stateArray objectAtIndex:row];
}
return nil;
}
答案 0 :(得分:1)
你在if中引用了你self.txtFillingStatus.tag
。
尝试引用选择器类。
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if ([pickerView isEqual:self.pickerFillStatus]) {
return self.fillArray.count;
} else if (([pickerView isEqual:self.pickerState]) {
return self.stateArray.count;
}
return 0;
}
答案 1 :(得分:0)
首先,没有必要创建2个选择器视图。您的功能只能通过一个选择器视图本身来实现。你需要做的是
1.创建一个选择器视图。例如
- (void) createPickerView {
pickerView = [[UIPickerView alloc] init];
[pickerView setDelegate:self];
[pickerView setDataSource:self];
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:2 inComponent:0 animated:YES];
}
2。在textFieldDidBeginEditing方法中,根据所选的文本字段重新加载pickerView组件。查看下面的代码以供参考。这里" currentTextfieldForPicker"是NSString * currentTextfieldForPicker; 。 " TXTFILLING_STATUS"和" TXT_STATE"是两个字符串,用于标识单击的文本字段。
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == txtFillingStatus) {
currentTextfieldForPicker = TXTFILLING_STATUS;
[pickerView reloadAllComponents];
}
else if (textField == txtState) {
currentTextfieldForPicker = TXT_STATE;
[pickerView reloadAllComponents];
}
}
3. Now implement picker view delegates as follows:
// Total rows in our component
-(NSInteger)pickerView:(UIPickerView*)pickerViewnumberOfRowsInComponent:(NSInteger)component {
if ([currentTextfieldForPicker isEqualToString:TXTFILLING_STATUS]) {
return [fillArray count];
}else if ([currentTextfieldForPicker isEqualToString:TXT_STATE]) {
return [stateArray count];
}
else {
// Some default array if you want to
return [inputDataArray count];
}
}
// Display each row's data.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if ([currentTextfieldForPicker isEqualToString:TXTFILLING_STATUS]) {
return [fillArray objectAtIndex:row] ;
} else if ([currentTextfieldForPicker isEqualToString:TXT_STATE]) {
return [stateArray objectAtIndex:row];
}
return [inputDataArray objectAtIndex:row] ;
}