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 UICon开发者_如何学运维trolEventEditingDidEndOnExit 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 UITextView
has a delegate protocol you should implement. The delegate protocol is naturally named UITextViewDelegate
. You should notice by browsing the documentation that there is a pattern; For any class Foo
that has a delegate protocol the protocol is always named FooDelegate
, there are no exception.
Now you can as James pointed out intercept text changes using the UITextView
s delegate and dismiss the keyboard. I would however discourage you from doing so, because it violates the Human Interface Guidelines. A UITextView
is 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 protocol UITextFieldDelegate
. This protocol has a method intended for what you want:
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
[textField resignFirstResponder];
return NO;
}
精彩评论