1

我有一个 Picker 作为 UITextField 的 inputView 而不是键盘。用户拨入他们的选择,保存然后关闭视图。当您返回该视图并拨入新选择时,选择器与保存的数据不同步。

选择器使用字符串数组作为数据源。

因此,例如查看下面的图片,您可以看到选择器位于它的第一个位置,该位置是空的。但我想要的是让选择器拨入 RE2 或保存在 textField 中的任何其他选项。

如何同步两者?

在此处输入图像描述

4

1 回答 1

3

我想到了。我遍历选择器的 dataSource 数组以找到匹配的字符串,然后在选择器上使用 selectRow:inComponent:animated 来同步它。这是代码。我在 textField 委托方法中触发它。

- (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];
}
于 2011-02-07T00:47:52.730 回答