1

我有几个UIPickerView相同的观点。当我在第一个中选择一个项目时UIPickerView,我想UIPickerView根据所选项目将数据加载到另一个项目。

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

我怎么能实现方法来做到这一点?

4

2 回答 2

0

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

[thePicker reloadAllComponents];
于 2013-05-01T22:25:13.663 回答
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)在这个例子中检查。

于 2013-05-01T22:29:56.817 回答