ioskeyboarduipopover

How to dismiss already opened keyboard when popover appears by other textfield ios


In my iOS project, I have a form which contains various text fields. Some text fields are edited by keyboard and some by picker view which is placed on the popover.

When I go on filling text fields, without dismissing it and then if I click on popover text field keyboard remains open.

It appears as both keyboard and popover present on screen at the same time, which I don't want.

I am able to get whether the keyboard is opened or not by setting a flag in keyboard notification methods and also the last text field that was edited through text filed delegates. And have tried

  1. [self endEditing: YES]; (as it is in a table cell)

  2. [lastEditedTextField resignFirstResponder];

Even try to pass keyboard dismiss the notification by my self (without knowing whether it is possible or not)

  1. [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillHideNotification object:nil];

but nothing is working.

How can I dismiss keyboard (if already open) whenever popover appears?


Solution

  • Try to implement textFieldShouldBeginEditing: and inside it check which text field is this. If it is one of the fields that should display a popover, first call [self.view endEditing:YES] to hide the keyboard, then present the popover and return NO. This way the text field won't take first responder status and the keyboard will not appear again. And if it is one of the "normal" text fields, just return YES.