依赖uipickerview

时间:2013-05-01 22:06:06

标签: ios objective-c uipickerview

我在同一个视图中有几个UIPickerView。当我在第一个UIPickerView中选择一个项目时,我想根据所选项目将数据加载到其他UIPickerView

示例:当我选择法国作为国家时,我应该选择城市选择城市(巴黎,图卢兹等)。

我怎样才能实现这样做的方法?

2 个答案:

答案 0 :(得分:0)

使用完第一个UIPickerView后,您可以将数据加载到其他UIPickerView中并调用:

[thePicker reloadAllComponents];

答案 1 :(得分:0)

如果您有对每个选择器视图的引用,那么您只需执行类似这样的操作

// These are instance variables within the view controller
NSString *selectedCountry;
NSArray *listOfCountries = @[@"France", @"Switzerland"];

// In the picker view delegate callback
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == countryPickerView) {
        selectedCountry = [listOfCountries objectAtIndex:row];
        [cityPickerView reloadAllComponents]; // Or you can reload individual components
    }
}

cityPickerView的数据源应该查找selectedCountry的值是什么,并使用该特定国家/地区的城市填充自己。


作为旁注,如果您在一个视图中有多个选择器视图,您可能需要考虑为每个UIPickerView创建一个子类,并为每个选择器视图创建子类委托/数据源,这样您就不会最终得到意大利面条了代码并且必须在此示例中检查if (pickerView == countryPickerView)