UIVIew endEditing:YES doesnt hide the keyboard

chost picture chost · Sep 14, 2012 · Viewed 7.7k times · Source

I have a UIVIew which is a subview and it contains several UITextFields. One of these textfields (which is for DATE) should not be editable using the keyboard, instead of this I use a PopOver with a Datepicker inside.

I run a method when the UIControlEventEditingDidBegin is reached. This method calls the resignFirstResponder on the DateTextField.

Everything works fine if the DateTextField is the first field to edit, but when another textField is edited and of course shows the keyboard and then try to edit the DateField, the keyboard doesn't hide and everything goes normal but with the Keyboard doing anything.

I have tried to call the method endEditing:YES before the resignFirstResponder but it doesn't work. I have tried to run the endEditing:YES and resignFirstResponder on the didEndEditing text field method but theres no way to get that keyboard out.

here is my method:

- (void)showDatePopOver:(id)sender{ 
    [self.view endEditing:YES];

    UITextField *textField = (UITextField *)sender;
    [sender resignFirstResponder]; // hide keyboard

    /** POP OVER LINES**/
}

Answer

chown picture chown · Sep 14, 2012

You should use the textFieldShouldBeginEditing: delegate method instead of resigning first responder in didBeginEditing:

This will allow editing on ALL BUT the dateTextField text field:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return (![textField isEqual:dateTextField]);
}

You should specify that your view controller is a text view delegate as well like so (in the interface declaration [.h file]):

@interface MyViewController : UIViewController <UITextFieldDelegate>