如何根据另一个选择器的选择更改选择器行? [英] How to change the picker rows based on another picker's selection?

查看:79
本文介绍了如何根据另一个选择器的选择更改选择器行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择器-国家和地区.我需要根据在国家/地区选择器中选择的行来填充地区选择器.我有数组来填充两个选择器.我需要一些帮助,以根据国家/地区选择器的选择更改区域选择器的行.任何帮助将不胜感激.提前谢谢.

I have two pickers - country and region. I need to populate the region picker based on the row selected in the country picker. I have arrays to populate both the pickers. I need some help to change the rows of region picker based on the country picker's selection. Any help would be greatly appreciated. Thanks a lot in advance.

推荐答案

好的,您有两个选择器,例如countryPicker和regionPicker.

Ok You have two pickers lets say countryPicker and regionPicker.

在UIPickerView的委托方法中添加一个条件

in the delegate method for UIPickerView add one condition

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ 
    NSString *pickerTitle = @"";
    if (pickerView == countryPicker)
    {
        pickerTitle =  [countryFeeds objectAtindex:row];
        //assigns the country title if pickerView is countryPicker
    }
    else if (pickerView == regionPicker)
    {
        pickerTitle =  [regionFeeds objectAtindex:row];
        //assigns the region title if pickerView is regionPicker
    }

    return pickerTitle;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == countryPicker)
    {
        //selects the region corresponding to the selected country.
        //totalRegions is a NSDictionary having country names as keys and array of their regions as values
        regionFeeds = [totalRegions objectForKey:[countryFeeds objectAtindex:row]];

        //Now reloading the regionPicker with new values.
        [regionPicker reloadAllComponents];
    }
    else if (pickerView == regionPicker)
    {
        // your code to select a region
    }
}

我希望这可以解决您的问题:)
BR,哈里

I hope this solves your problem :)
BR, Hari

这篇关于如何根据另一个选择器的选择更改选择器行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆