且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何根据另一个选择器的选择更改选择器行?

更新时间:2022-04-17 22:00:28

好的,您有两个选择器,例如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