UITextView doesn’t inherit from UIControl, so there are not addTarget:forControlEvent: methods possible. I used to register an action method just to resign first responder when UIControlEventEditingDidEndOnExit happens.
How can I do that with an UITextView? Is there some delegate + protocol I must implement so I can make the keyboard go away?
Yes
UITextViewhas a delegate protocol you should implement. The delegate protocol is naturally namedUITextViewDelegate. You should notice by browsing the documentation that there is a pattern; For any classFoothat has a delegate protocol the protocol is always namedFooDelegate, there are no exception.Now you can as James pointed out intercept text changes using the
UITextViews delegate and dismiss the keyboard. I would however discourage you from doing so, because it violates the Human Interface Guidelines. AUITextViewis intended for editing text with multiple lines of text, intercepting the enter key to mean something elsa than line breaks is not advised.If you want a text field with only a single line of text, or being dismissed by enter key, then you should instead use a
UITextField, which also have a delegate protocolUITextFieldDelegate. This protocol has a method intended for what you want: