我正在创建一个选择器视图,当用户点击文本字段时会显示该视图。 它工作正常。但是当用户点击自定义完成按钮时,我想忽略选择器视图。到目前为止,这是我的代码:
- (void)showPickerWithDoneButton:(UITextField *)sender
{
UITextField *textField = sender;
// Creamos UIPickerView como una vista personalizada de un keyboard View
UIPickerView *pickerView = [[UIPickerView alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
//UIPickerView
//Asignamos el pickerview al inputView de nuestro texfield
self.tipos_auto.inputView = pickerView;
// Preparamos el botón
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleDefault;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"Aceptar", @"Button") style:UIBarButtonItemStyleBordered target:self action:@selector(pickerHechoClicked:)];
doneButton.tintColor = [UIColor blackColor];
//Para ponerlo a la derecha del todo voy a crear un botón de tipo Fixed Space
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpace.width = keyboardDoneButtonView.frame.size.width - 150;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:fixedSpace, doneButton, nil]];
// Finalmente colocamos la keyboardDoneButtonView en el text field...
textField.inputAccessoryView = keyboardDoneButtonView;
}
这是应该忽略选择器视图的方法:
-(void) pickerHechoClicked :(id)sender{
[sender resignFirstResponder];
}
但点击按钮后,应用程序崩溃并出现以下错误:
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem resignFirstResponder]: unrecognized selector sent to instance
欢迎任何帮助。
答案 0 :(得分:4)
最简单的方法是让视图完成所有编辑操作,因为那时当前的第一响应者实际上并不重要(只要它是某个地方的子视图):
[self.view endEditing:YES];
答案 1 :(得分:2)
看起来您正在将UIBarButtonItem传递给“pickerHechoClicked”方法,而不是发送您的UIPickerView实例。
当按下完成按钮时,你应该将pickerView变量作为参数传递给“pickerHechoClicked”。
我没有看到您实际为自定义完成按钮分配操作的代码,但在代码中处理按钮按下时使用此:
[self pickerHechoClicked:pickerView];
答案 2 :(得分:0)
您不能在您的UIBarbuttonItem上调用resignFirstResponder,而是调用您的选择器。所以简单的解决方案是保留你的选择器的引用然后调用[self.myPicker resignFirstResponder]