开发者

How to react on the DONE button of the keyboard when using a UITextView?

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 UITextViews 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;
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜