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

IosObjective CCocoa TouchUipickerview

Ios Problem Overview


enter image description here

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.

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

Swift: xcode 6.1

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

[self.pickerControl reloadAllComponents];

Solution 3 - Ios

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

Solution 4 - Ios

By calling

thePicker.delegate = self;

again, I was able to force refresh the pickerview with new info

Solution 5 - Ios

Use multiple pickers, or use a switch block to determine which data should be loaded, and reload the picker.

Solution 6 - Ios

In your button press action,

[self.pickerView selectRow:0 inComponent:0 animated:YES];

Solution 7 - Ios

Swift 3 & 4

I had the same problem but no solution works,

then i use DispatchQueue it works

DispatchQueue.main.async(execute: {
        self.picker.delegate = self
        self.picker.dataSource = self
        if self.pickerData.count > 0 {
            self.pickerTextField.text = self.pickerData[0]
            self.pickerTextField.isEnabled = true
        } else {
            self.pickerTextField.text = nil
            self.pickerTextField.isEnabled = false
        }
    })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjim4iOSView Question on Stackoverflow
Solution 1 - IosvikingosegundoView Answer on Stackoverflow
Solution 2 - IosVinod JoshiView Answer on Stackoverflow
Solution 3 - IosEnamul HaqueView Answer on Stackoverflow
Solution 4 - IosJuan ManuelView Answer on Stackoverflow
Solution 5 - IosWrightsCSView Answer on Stackoverflow
Solution 6 - IosJeff JohnView Answer on Stackoverflow
Solution 7 - IosSumitView Answer on Stackoverflow