I've got a Picker that comes up as the inputView for UITextField instead of a keyboard. The user dials in their choice, saves and then dismisses the view. When you come back to that view and go to dial in a new choice the picker is out of sync with the data that was saved.
The picker is using an array of strings for a datasource.
So for example looking at the picture below you can see that the picker is in it's first position which is empty. But what I want is for the picker to dial in to RE2 or whichever of the other options is saved in the textField.
How do I sync the two up?
I figured it out. I iterated through the dataSource array for the picker to find the matching string then used selectRow:inComponent:animated on the picker to sync it up. Here's the code. I was triggering it in a textField delegate method.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
UIBarButtonItem *saveButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = saveButtonItem;
[saveButtonItem release];
//Sync The Picker
for(NSInteger i = 0; i < [pickerData count]; i++){
NSString *string = [pickerData objectAtIndex:i];
if([string isEqualToString:profileField.text]){
pickerRow = i;
break; //Once we have it break out of the loop
}
}
[myPicker selectRow:pickerRow inComponent:0 animated:NO];
}
来源:https://stackoverflow.com/questions/4917186/how-to-sync-selected-row-of-uipicker-with-previously-entered-data