3
votes

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?

2
Possible duplicate of How do I dismiss the iOS keyboard?Cœur
@Cœur, your question was just about dismissing the keyboard but under what scenarios is not clarified (just vague sentence). Kindly go through the scenario which is mentioned in the problem. It is not just about keyboard dismiss. Keyboard dismiss for UITextField which will be edited by picker embed in the popover. As mentioned in the accepted answer, calling keyboard dismiss functionality in textFieldShouldBeginEditing method with a check for the currently editing text field is very important for the mentioned scenario.Chetan Koli
I've retracted my close vote, but I still don't understand what is unique about your question: you want to dismiss keyboard on click, then just dismiss keyboard on click. As you're comparing dismissing methods, just refer to the linked possible duplicate for how to do it.Cœur

2 Answers

2
votes

You can call:

[self.view endEditing:YES];

But, a better solution is likely to present the picker using the UIResponder inputView so it automatically replaces the keyboard and you don't need to mediate between 2 different things (and the user doesn't switch between different parts of the screen potentially).

0
votes

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.