Ios 点击“完成”按钮时关闭UIPickerView

Ios 点击“完成”按钮时关闭UIPickerView,ios,Ios,我正在创建一个选择器视图,当用户点击文本字段时显示该视图。 它很好用。但我想在用户点击custom Done按钮时关闭picker视图。这是我目前的代码: - (void)showPickerWithDoneButton:(UITextField *)sender { UITextField *textField = sender; // Creamos UIPickerView como una vista personalizada de un keyboard View

我正在创建一个选择器视图,当用户点击文本字段时显示该视图。 它很好用。但我想在用户点击custom Done按钮时关闭picker视图。这是我目前的代码:

- (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 

欢迎提供任何帮助。

看起来您正在向UIBarButtonim传递“PickerEchoClicked”方法,而不是发送UIPickerView实例

按下“完成”按钮时,应将pickerView变量作为参数传递给“PickerEchoClicked”

我看不到您实际为自定义完成按钮指定操作的代码,但在处理按钮按下的代码中,请使用以下内容:

[self pickerHechoClicked:pickerView];

看起来您正在将“PickerEchoClicked”方法传递给UIBarButtonim,而不是发送UIPickerView实例

按下“完成”按钮时,应将pickerView变量作为参数传递给“PickerEchoClicked”

我看不到您实际为自定义完成按钮指定操作的代码,但在处理按钮按下的代码中,请使用以下内容:

[self pickerHechoClicked:pickerView];

最简单的方法是要求视图完成所有编辑操作,因为这样无论当前的第一响应者是什么(只要它是某个地方的子视图):


最简单的方法是要求视图完成所有编辑操作,因为这样无论当前的第一响应者是什么(只要它是某个地方的子视图):


你不能在你的uibarbuttoneim上给辞职的FirstResponder打电话,而是打给你的选择者。因此,简单的解决方案是保留您的选取者的引用,然后调用[self.myPicker resignFirstResponder]

您不能在UIBarbuttonItem上调用resignFirstResponder,而是调用您的选取者。因此,简单的解决方案是保留您的选取者的参考资料,然后致电[self.myPicker resignFirstResponder]

谢谢。但我相信Wain的回答是最好的解决方案。谢谢。但是我相信Wain的答案是最好的解决方案。啊,我不知道UIView的这个功能。美好的啊,我不知道UIView的这个功能。美好的