Chetan Koli
Chetan Koli

Reputation: 188

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?

Upvotes: 2

Views: 1161

Answers (2)

Lubo
Lubo

Reputation: 304

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.

Upvotes: 1

Wain
Wain

Reputation: 119031

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

Upvotes: 1

Related Questions