views:

962

answers:

6

I'm trying to hide the iPad keyboard from a modal view controller but it doesn't work. I have tried resignFirstResponder but that doesn't have any affect if we are in a modal view controller. I tried resignFirstResponder in a non-modal UINavigationController with the very same UIViewController and the keyboard hides correctly.

Does anyone know how solve this problem?

Thanks.

[Update] it looks like there's something wrong with my code because the resignFirstResponder does work (I made a simple test case instead of using my code). But I still don't know what the problem is.

+5  A: 

It was because I was using UIModalPresentationFormSheet. All of the other ones work as expected.... Wasted several hours on that.

Cal
I've been having the exact same problem - is this a bug with UIModalPresentationFormSheet?
davbryn
I guess they assume if you're doing form entry you will never want to hide the keyboard... I ended up changing it to a non-modal view controller in my case.
Cal
+3  A: 

I just confirmed the problem is indeed UIModalPresentationFormSheet and filed a bug report to apple rdar://8084017

Alej
Sucks that we have to wait for 4.0 to come out for iPad in "the fall" (whenever that is)
Sam Soffes
@alej: May i ask, have you gotten a feedback from apple regarding the bug you reported?
thatsdisgusting
A: 

Thanks for this comment, because even i wasted several days fixing this problem.

Inal
This is not an answer. Please comment on questions or answers using a comment.
Sam Soffes
A: 

Thanks for your post... Dem waste mine 3 hours... When the apple gonna fixed it?

Ahmad Ashraf bin Azman
A: 

I am seeing the same problem with a textfield in a popover. The bottom right button to hide the keyboard doesn't work. Is there a way to code for that?

kaur
+1  A: 

My workaround for this annoying bug:

[myTextField resignFirstResponder];
@try
{
    Class UIKeyboardImpl = NSClassFromString(@"UIKeyboardImpl");
    id activeInstance = [UIKeyboardImpl performSelector:@selector(activeInstance)];
    [activeInstance performSelector:@selector(dismissKeyboard)];
}
@catch (NSException *exception)
{
    NSLog(@"%@", exception);
}
0xced
Wouldn't you get rejected for using a private API if you do this?
Cal
Maybe, I have not tried to submit this code to the App Store. But notice the @try/@catch: this code is safe to use. If the `activeInstance` and/or `dismissKeyboard` methods are removed or renamed, the unrecognized selector exception will be caught, and your app will not crash.
0xced
Nice solution, I just came across this issue recently and I was worried Apple might reject the app -because- of the fact the keyboard does not hide when the Return button is tapped when using UIModalPresentationFormSheet. I don't really want to use UIModalPresentationPageSheet because it covers the entire screen and is too big for my requirements. So, if one tries to submit this (or any other Private API) how do apple detect it? Do they de-compile it or do they only detect it by virtue of a crash when try/catch isn't used?
EagleOfToledo
I suspect they have an automated tool that "decompiles" your code. I was rejected once for using a private API and I believe I used the same performSelector approach as above. I was using a private API to find the currently focused component and then calling resignFirstResponder.
Cal
Just curious 0xced... did you submit this in a live app and were approved?
Cal
No, I did not. You can construct the selector dynamically with NSSelectorFromString().
0xced
hmm, I wouldn't even try. Just wait for iOS 4 I guess.
William Denniss