How to Sync selected row of UIPicker with previously entered data

痴心易碎 提交于 2019-12-23 03:59:16

问题


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?


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!