mosdev
mosdev

Reputation: 217

iPad popover textfield - resignFirstResponder doesn't dismiss keyboard

I have two text fields email and password. The following code works fine when the fields are presented on a regular view but when they are on a popover, the resignFirstResponder does not work (becomeFirstResponder works). textFieldsShouldReturn was called for both fields. Any idea if I am missing something? Thanks!

  - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

     if (theTextField == email) {
         [password becomeFirstResponder];
         return NO;
     }

     [theTextField resignFirstResponder];
     return NO;
}

Upvotes: 15

Views: 9179

Answers (5)

nspire
nspire

Reputation: 1721

The answer is provided as a possible solution to others with a similar problem, but where the conventional remedies don't work.

In summary -

I had a similar problem (under a certain condition) and tried everything - to no avail - Included in my list of possible solutions was [obj's resignFirstResponder], the overriding of 'disablesAutomaticKeyboardDismissal' for my view controller, [self.view endEditing:YES]; and a bunch of other things.

Went about determining the [id] of the current first responder, only to discover it was nil. Tapping 'Done' on the keyboard or using any of the methods above did nothing - the keyboard remained - even after tapping on another input field.

The screen was essentially a ViewController with a UITableView with a text input field in each cell - 7 or 8 in total. Tapping on any cell would bring up keyboard as expected and tapping a separate 'Next' button (to hide the keyboard plus other processing) worked as expected. However, in landscape mode, the last field was covered by the keyboard requiring the table to be scrolled to reveal such.

After scrolling and tapping that last input field, the keyboard could not be dismissed - no matter what. The only work around was to scroll the table back under the keyboard, then tap the 'next' button. It doesn't make sense.

Almost at the point of giving up (and implementing a workaround), the solution that worked was to make that last input field the firstResponder (even though it already had a blinking cursor) and then to resignFirstResponder after that.

So;

`-(void) actionNext {

[[m_arrInputFields objectAtIndex:7] becomeFirstResponder];
[[m_arrInputFields objectAtIndex:7] resignFirstResponder];

}`

fixed the problem - whereas [m_arrInputFields objectAtIndex:#any other index#] did not!

Would be great if anyone can provide clarity or an explanation for this - else - I hope it saves someone else a few hours of work!

Upvotes: 0

aslı
aslı

Reputation: 8914

Check this question:

Overriding disablesAutomaticKeyboardDismissal to return NO as below fixed the same problem of mine. You should put this code to your view controller, from which you initiate the keyboard:

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

Upvotes: 17

Lachlan Roche
Lachlan Roche

Reputation: 25956

As described in this answer, the keyboard will sometimes remain on-screen when the view is presented with the UIModalPresentationFormSheet style.

Upvotes: 2

YPK
YPK

Reputation: 1851

I was also having this problem. But I solved this by making a another control, which is not in the popover as firstResponder and later a resigned it from there. But I don't what is the problem with popover.

Upvotes: 1

jv42
jv42

Reputation: 8593

I'm not too sure about this, but, as I understand the responder hierarchy, resign would work only if you have some other responder to answer.

In a regular view, the view itself is willing. In a popup, maybe you need to do something to your popup class (like reimplement some Responder methods) in order for this to work.

Upvotes: 1

Related Questions