7

I am trying to hide the keyboard in my SplitView application (because it covers over part of the root menu). However, the only thing I can find is how to hide the keyboard after a textfield has been used [TextField resignFirstResponder].

Is there any other way to hide the keyboard?
Ideally I would like to use the barButtonItem, which displays the menu, as a trigger to hide the keyboard.

BloonsTowerDefence
  • 1,184
  • 2
  • 18
  • 43

3 Answers3

13

Use this:

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
0

You need to send the -resignFirstResponder message to the instance of whatever UI element currently has first responder status. So if there were a firstNameTextField property on your class that corresponds to an instance of UITextField, you need to send the message to that object.

[self.firstNameTextField resignFirstResponder];

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • Does it work with any UI element? Can I use a UIBarButtonItem? My whole problem is that I don't have a specific textfield to link it to. – BloonsTowerDefence Aug 15 '12 at 17:46
  • Technically any instance of `UIView` can become the first responder since it is a subclass of `UIResponder` but it only invokes the keyboard if the class conforms to the `UITextInput` protocol. To dismiss the keyboard via a `UIBarButtonItem`, you need to connect that button to a method that invokes `resignFirstResponder` on the active text field. If you just want to blindly end all editing, you can also send `endEditing:` to the root view and it will walk through the subviews and resign first responder on which ever subview currently has it. – Mark Adams Aug 15 '12 at 17:50
0

resignFirstResponder is way to do it. If you have a situation where your firstResponder is not set up as an instance variable (perhaps its generated), you can 'get' your firstResponder using this answer. After you have your first responder object, simply resign it!

hope this helps.

Community
  • 1
  • 1
KDaker
  • 5,899
  • 5
  • 31
  • 44