Objective c 自动选择UIPickerView位置

Objective c 自动选择UIPickerView位置,objective-c,uipickerview,Objective C,Uipickerview,我对两个文本字段使用一个UIPickerView。一个是国家,另一个是省。我现在遇到的问题是,当我选择国家并返回到省UIPickerView时,它将与国家放在相同的索引位置 这是代码 PickerView didSelectedRow事件 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { selectedpickerDat

我对两个文本字段使用一个UIPickerView。一个是国家,另一个是省。我现在遇到的问题是,当我选择国家并返回到省UIPickerView时,它将与国家放在相同的索引位置

这是代码

PickerView didSelectedRow事件

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

selectedpickerData = [NSString stringWithFormat:@"%@", [pickerData objectAtIndex:row], row];

// Editing Country
if (activePickerField == 1) {
    txtCountry.text = selectedpickerData;
}
// Editing Province
else if (activePickerField == 2) {
    txtProvince.text = selectedpickerData;
}
}
问题1:如何保持每个字段的索引位置


问题2:文本字段为空时,如何选择UIPickerView的第一个索引位置?

UIPickerView有方法

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
您可以使用它选择0作为所选索引


然后,对于第二个问题,您需要验证[UITextField text:isEqualToString:@”“],然后调用上面的方法。例如,当某些控件退出第一响应程序时,可能会发生这种情况。

对不起,如何将选定的选择器索引分配给字段?我更新了代码。请指出这一点。selectedpickerData=[NSString stringWithFormat:@“%@,[pickerData objectAtIndex:row];然后,您可以在if add when textfield 1中选择将所选索引保存到某个定义的int变量,并对第二个变量执行相同的操作