Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Input Keyboard on iPhone Without Knowing First Responder?

I have seen this question, but the question is how to know which textView is the first responder? This question looked promising to figure out the first responder, but it turns out that it calls private APIs. Is there any way to hide the keyboard or to figure out the first responder as the-one-who-has-the-keyboard?

like image 495
Dan Rosenstark Avatar asked Jul 01 '10 20:07

Dan Rosenstark


3 Answers

It's easy:

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

take a look at UIView Class Reference.

like image 195
Andrey Zverev Avatar answered Sep 21 '22 14:09

Andrey Zverev


[textView isFirstResponder] should tell you if it's the first responder. I guess you could just loop through all text fields in your class to check if it's the first responder.

Or you could just call resignFirstResponder on every textField, and it'll still work.

like image 4
Tejaswi Yerukalapudi Avatar answered Sep 20 '22 14:09

Tejaswi Yerukalapudi


I believe you should pick something to become the first responder, something which probably doesn't do anything on its own, and call -[UIResponder becomeFirstResponder] on it. This would work well with a UIViewController or UIWindow (which you can get through -[UIView window], I think) because these would have the most similar responder chains, and would make the current firstResponder loose its firstResponder status, dismissing the keyboard, but also wouldn't bring up the keyboard, as it would not be a UITextField.

like image 3
Jared Pochtar Avatar answered Sep 22 '22 14:09

Jared Pochtar