gpt4 book ai didi

ios - 如何在同一类中的两个选择器 View 之间切换

转载 作者:行者123 更新时间:2023-11-29 01:20:31 25 4
gpt4 key购买 nike

我使用了 2 个数组来在各自的选取器 View 中传递数据。我已经通过了 UITextField 上的选取器 View 。我在特定文本字段选择上有 2 个文本字段,关联的选择器 View 应该出现。我尝试了一些方法仍然没有结果。

代码如下:

 //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;
}

最佳答案

您在 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;
}

关于ios - 如何在同一类中的两个选择器 View 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34653788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com