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.
-
you searched on google or SO? there are so many links available – Yama Jan 19 '12 at 11:01
4 Answers
Ok You have two pickers lets say countryPicker and regionPicker.
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
}
}
I hope this solves your problem :)
BR, Hari

- 2,256
- 18
- 21
Are they separate picker views, or one picker view with two columns?
Either way, when you change the value in one picker, just call reloadAllComponents on the other picker (or reloadComponent: if you are using a split picker) and then when it reloads the data from it's data source you can use the value from the first picker to supply the correct region list.

- 40,865
- 11
- 112
- 103
-
Sorry for the late reply.Yeah I made a plist of countries and regions and used reloadComponent: on the region picker using NSDictionary to get all the regions of the selected country.Thanks all:) – confucizious Mar 02 '12 at 13:05
My suggestion is please maintain the country and region in one picker with two components(country,region).If u do like this then we can change the one component values based on another component value.

- 131
- 1
- 1
- 7
-
I could have done that but I'm supposed to whatever I'm told to do at work. :( – confucizious Mar 02 '12 at 13:08
NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];
NSString *userchoice =[self pickerView:CountryPicker titleForRow:selectedRow forComponent:0];
This will give you the title for the selected row by the user in country picker
then using the arrays you have to populate names of regions You can use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
on the RegionPicker

- 21,721
- 8
- 65
- 83