配置"完成"多个选择器视图的按钮

时间:2014-07-18 19:57:48

标签: ios objective-c uipickerview

我有一个场景,我想使用选择器视图来获取多个文本框的输入。我希望每个选择器视图都有一个带有" Done"取消选择器视图的按钮。到目前为止,我有:

//toolbar with "Done" button for picker views
UIToolbar *pickerToolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[pickerToolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                 style:UIBarButtonItemStyleBordered
                                                                 target:self
                                                                 action:@selector(doneWithPicker:)];
pickerToolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];

//set up picker views
_pickerOne = [[UIPickerView alloc] init];
_pickerOne.dataSource = self;
_pickerOne.delegate = self;
self.textFieldOne.inputView = self.pickerOne;
self.textFieldOne.inputAccessoryView = pickerToolBar;

我需要的是doneWithPicker方法找出当前正在编辑哪个文本字段并调用resignFirstResponder的方法。

1 个答案:

答案 0 :(得分:0)

使用文本字段委托来标识哪个字段处于活动状态。 (确保将自己设置为代表)

@implementation WhateverViewController
{
    UITextField *activeTextField;
}

...

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeTextField = textField;
}

然后,在点击完成按钮时调用的方法:

[activeTextField resignFirstResponder];