Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIVIew endEditing:YES doesnt hide the keyboard

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**/
}
like image 849
chost Avatar asked Nov 04 '22 16:11

chost


1 Answers

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>
like image 166
chown Avatar answered Nov 10 '22 04:11

chown