0

I'm new to iOS development and have recently learned that to make the on screen keyboard disappear we must always call resignFirstResponder on the text view. This causes the text view to relinquish it's first responder status, and hence the keyboard disappears, since there is no need for the text view to respond.

However I also noticed that there's the becomeFirstResponder method to make a view a first responder. However, this method is never called on the text view. So when/how does a textview become first responder when that method is never called?(at least, by me. I'm unsure if it is called elsewhere in the system)

My theory is that is has to already be a first responder before it can resign the first responder status.

sosale151
  • 340
  • 2
  • 4
  • 19
  • 1
    It is performed for you when a user taps on the text field. You can monitor for it using the delegate method `textViewDidBeginEditiing` or more broadly by listening for keyboard notifications (`UIKeyboardWillShowNotification` for example) – Louis Tur Mar 16 '15 at 18:05
  • So it happens automatically i.e. the system takes care of it for me by calling the becomeFirstResponderMethod when a user taps on the text field? – sosale151 Mar 16 '15 at 18:14
  • Yes. So long as user interaction is enabled for the textField/View, the keyboard should pop up when tapped. There are ways to handle dismissing it as well without the need of calling `resignFirstResponder` (such as calling `endEditing:` on a container view, or setting a scroll view's `UIScrollViewKeyboardDismissMode`). In the simulator, it's possible that the keyboard doesn't show up still, and in that case you just want to make sure keyboard hardware is toggled (Cmd+K) – Louis Tur Mar 16 '15 at 18:20
  • Your comment answered my question. If you want to "answer" this question, so that I can mark it as the correct answer (thereby giving you more reputation), you're welcome to do so. – sosale151 Mar 16 '15 at 18:23
  • thank you, kindly. answer has been posted – Louis Tur Mar 16 '15 at 18:30

2 Answers2

1

firstResponder status is automatically handled for you when a user taps on the text field. So long as user interaction is enabled for the UITextField/UITextView, the keyboard should appear when tapped.

You can monitor for it using the delegate method textViewDidBeginEditiing or, more broadly, by listening for keyboard appearance notifications (UIKeyboardWillShowNotification and UIKeyboardDidShowNotification).

Further, there are ways to dismiss the keyboard without the need of calling the corresponding method resignFirstResponder (such as calling endEditing: on a container view, or setting a scroll view's UIScrollViewKeyboardDismissMode).

Note: In the simulator, it is possible that the keyboard still doesn't appear even if all is correctly working. In that case you just want to make sure keyboard hardware is toggled (CMD+K) for the simulator.

Community
  • 1
  • 1
Louis Tur
  • 1,223
  • 9
  • 16
0

You call becomeFirstResponder yourself if you want to give a UITextField focus and show the keyboard.

This is useful for view controllers that are basically forms. For example, if a user presses a "Sign Up" button, you might push a view controller with a couple of text fields and call becomeFirstResponder on the first one to automatically give it focus and open the keyboard.

Ghazgkull
  • 934
  • 1
  • 6
  • 15