How do I reload/refresh the UIPickerView (with new data array) based on button press? How do I reload/refresh the UIPickerView (with new data array) based on button press? ios ios

How do I reload/refresh the UIPickerView (with new data array) based on button press?


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];}


Swift: xcode 6.1

// reload the component of picker (Used this inside the trigger action or button)

[self.pickerControl reloadAllComponents];


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();}