Reputation: 49
I am creating a picker view that is shown when the user taps on a text field. It works fine. But I want to dismiss the picker view when the user taps on the custom Done button. This is my code so far:
- (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;
}
And this is the method that should dismiss the picker view:
-(void) pickerHechoClicked :(id)sender{
[sender resignFirstResponder];
}
But after tapping on the button the app crashes with following error:
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem resignFirstResponder]: unrecognized selector sent to instance
Any help is welcome.
Upvotes: 1
Views: 3440
Reputation: 15784
You cannot call resignFirstResponder on your UIBarbuttonItem but to your picker. So the simple solution is to keep a reference of your picker then call [self.myPicker resignFirstResponder]
Upvotes: 0
Reputation: 2967
It looks like you are passing the UIBarButtonItem the "pickerHechoClicked" method instead of sending your instance of UIPickerView.
When the done button is pressed you should pass the pickerView variable as the parameter to "pickerHechoClicked" instead.
I do not see the code where you actually assign your custom done button an action but in the code to handle the button press use this:
[self pickerHechoClicked:pickerView];
Upvotes: 2
Reputation: 119021
The easiest thing to do is to ask the view to finalise all editing operations, because then it doesn't matter what the current first responder actually is (so long as it's a subview somewhere):
[self.view endEditing:YES];
Upvotes: 3