3

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?

Chetan Koli
  • 178
  • 15
  • Possible duplicate of [How do I dismiss the iOS keyboard?](https://stackoverflow.com/questions/6906246/how-do-i-dismiss-the-ios-keyboard) – Cœur Jun 06 '18 at 14:57
  • @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 Jun 08 '18 at 07:27
  • 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 Jun 08 '18 at 08:38

2 Answers2

2

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).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I have tried this one also it is not working, and my requirement is to show popover with picker view on it. So in that case your second solution of UIResponder, inputView will not work as it takes UIView as argument not a UIPopoverController – Chetan Koli Jun 04 '15 at 14:09
  • Explain where the first responder text field is and where you're calling endEditing (your line in the question is not like mine in the answer) – Wain Jun 04 '15 at 14:25
  • Inside -(void)textFieldDidBeginEditing:(UITextField *)textField method, where I am checking whether it is popover text filed or not, I have tried all these code (one at a time) – Chetan Koli Jun 05 '15 at 04:26
0

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.

Lubo
  • 192
  • 3
  • 10
  • I tried your suggestion, now if keyboard is already present on screen it gets disappears when I click on it and then popover appears. And when I directly start with popover text filed, first keyboard appears and then after clicking on it it gets disappears and then popover appears – Chetan Koli Jun 05 '15 at 04:35
  • @ChetanKoli, I suppose you're not using `textFieldShouldBeginEditing:` but some of the other methods of `UITextFieldDelegate`. You have to use `textFieldShouldBeginEditing:`. That way you control if the keyboard is going to appear or not by returning `YES` or `NO`, respectively. – Lubo Jun 05 '15 at 09:04
  • thank you so much... before this post I was handling this in -(void)textFieldDidBeginEditing:(UITextField *)textField method and after your comment I corrected my mistake. Now its working fine. – Chetan Koli Jun 05 '15 at 10:37
  • This put me on the right track! For my situation I needed self.view endEditing in both textFieldShouldBeginEditing and textFieldDidBeginEditing. More importantly, it gave me my weekend back! :D – PruitIgoe Sep 24 '16 at 12:14