将UIPickerView的值获取到UITextField,并在旋转时将其保持在底部



Goodday,

我遇到了以下问题:

在我看来,我得到了一个texfield,我正在代码中创建它。UITextfield正在这样的代码中构建:

UITextField *filiaalSelection = [[UITextField alloc] init];
filiaalSelection.frame = CGRectMake(130, 100, 200, 30);
filiaalSelection.borderStyle = UITextBorderStyleLine;
filiaalSelection.delegate = self;

现在,当我开始编辑文本字段时,需要弹出一个UIPickerView。我在以下代码中这样做:

-(void)textFieldDidBeginEditing:(UITextField *)filiaalSelection {
[filiaalSelection resignFirstResponder];
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, scrollView.frame.size.height - 225, scrollView.frame.size.width, 180)];
[myPicker setDelegate:self];
[myPicker setDataSource:self];
myPicker.showsSelectionIndicator = YES;
[self.view addSubview:myPicker];

}

现在我需要做两件事。第一件事是:获取UIPickerView的值并将其插入UITextField。以下方法返回所选UIPickerView的当前值:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [stringsArray objectAtIndex:row];
}

但问题是,我不知道如何将值发送到文本字段。我还需要在UIPickerView上面有一个done按钮。最好的方法是什么?

有什么理由不能这么做吗?

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [filiaalSelection setText:(NSString *)[stringsArray objectAtIndex:row]];
}

至于done按钮,为了方便您自己,您应该让选择器查看文本字段inputView。然后,当字段开始或结束编辑时(当调用[textfield resignFirstResponder];时(,它将自动显示和取消。

然后,您可以将按钮添加到视图的某个位置,也可以将其设置为文本字段的inputAccessoryView(如果希望将其附加到选取器视图的顶部(。

最新更新