0
votes

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.

2
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
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
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
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
thank you, kindly. answer has been postedLouis Tur

2 Answers

1
votes

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.

0
votes

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.