How to reset UIPickerView index: 0, iPhone

I am trying to reset a button click on a UIPickerView . My pickerview id is created at runtime, I already have delegates set. After googling I found

 [pickerView reloadAllComponents]; 

But this causes my app to crash every time it reaches here.

The object at index 0 is Select From List, and then the items. When the submit button is clicked, I want the β€œSelect from list” to stay at the top of my label (selected index: 0).

Here is my code

 ViewDidload pickerView = [[UIPickerView alloc] init]; pickerView.delegate = self; pickerView.dataSource = self; -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } // Total rows in our component. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return [nameArray count]; } // Display each row data. -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [nameArray objectAtIndex: row]; } // Do something with the selected row. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ dlbl.hidden=YES; NSLog(@"You selected this: %@", [nameArray objectAtIndex: row]); [btnSelectDesigner setTitle:[nameArray objectAtIndex: row] forState:UIControlStateNormal]; } 

and, when the button is pressed:

 -(IBAction)btnSubmitClicked:(id)sender{ [pickerView reloadAllComponents]; } 

Any idea what I'm doing wrong?

thanks

+7
source share
3 answers
 [picker reloadAllComponents]; [picker selectRow:0 inComponent:0 animated:YES]; 
+26
source
 //use this line for going at the top of the label index 0: [picker selectRow:0 inComponent:0 animated:YES]; 
+5
source

Quick version:

picker.selectRow(0, inComponent: 0, animated: true)

+3
source

All Articles