IF I wanted a picker, for states/provinces, i haven't seen an example, but I mocked up shown above, a picker for US/Can/Mex. wondering can you dynamically switch the NSMutableArray for the UIPickerView and then have it somehow reload each time you click on US/Can/Mex buttons??? How do I go about doing this. What approach do I take. Looking for someone to point a beginner for a clue in the right direction.
Asked
Active
Viewed 7.4k times
6 Answers
130
You will have to implement the datasource and the delegate.
once you press a button, set an array pointer to the appropriate array.
than call [thePicker reloadAllComponents];
-(IBAction) usButtonPressed:(id)sender{
self.inputArray = self.usArray;
[self.thePicker reloadAllComponents];
}
-(IBAction) mexicoButtonPressed:(id)sender{
self.inputArray = self.mexicoArray;
[self.thePicker reloadAllComponents];
}
the datasource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.inputArray count];
}
the delegate methods:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.inputArray objectAtIndex:row];
}

vikingosegundo
- 52,040
- 14
- 137
- 178
-
Thanks for pointing me in the right direction. I'm a newbie. Ok great to see there's a class method for it. and that it "Calling this method causes the picker view to query the delegate for new data for all components" very cool. My current UI Picker has the datasource and delegate methods :-) so thats good. I'm usinga a book, didn't know the answer was right in the instance methods of the Class documentation. Thanks again. – jim4iOS Dec 22 '11 at 05:32
30
Swift: xcode 6.1
// reload the component of picker (Used this inside the trigger action or button)
[self.pickerControl reloadAllComponents];

Naresh
- 16,698
- 6
- 112
- 113

Vinod Joshi
- 7,696
- 1
- 50
- 51
9
In swift 3,4,or 5 you just write in didSelectRow function like bellow
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.view.endEditing(false)
yourPickerView.selectRow(0, inComponent: 0, animated: true)
yourPickerView.reloadAllComponents();
}

Enamul Haque
- 4,789
- 1
- 37
- 50
7
By calling
thePicker.delegate = self;
again, I was able to force refresh the pickerview with new info

Juan Manuel
- 239
- 2
- 4
2
In your button press action,
[self.pickerView selectRow:0 inComponent:0 animated:YES];

Jeff John
- 31
- 3
2
Use multiple pickers, or use a switch block to determine which data should be loaded, and reload the picker.

WrightsCS
- 50,551
- 22
- 134
- 186