Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 604
jim4iOS Avatar asked Dec 22 '11 00:12

jim4iOS


2 Answers

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]; } 
like image 170
vikingosegundo Avatar answered Sep 16 '22 16:09

vikingosegundo


Swift: xcode 6.1

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

[self.pickerControl reloadAllComponents]; 
like image 24
Vinod Joshi Avatar answered Sep 16 '22 16:09

Vinod Joshi