tags:

views:

31

answers:

2

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?

A: 

You should use the UITextViewDelegate methods:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html

Just set myTextView.delegate = self, and place the protocol here: ClassName : UIViewController <UITextViewDelegate> in the header file.

Evan Mulawski
+1  A: 

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;
}
PeyloW
Good point, but how do you officially finish editing an UITextView when you can't use the ENTER key for this? In my case, the ENTER kex appears blue as DONE button. If I change that to Enter key, then how to dismiss that keyboard?
BugAlert
You dismiss the keyboard using `-[UITextView resignFirstResponder]`. Just do it using another user action than a return key. You could add a "Done" button to the navigation bar, or a "Done" button on the `inputView` of a `UITextField` or `UITextField`. This is the view that for example Safari uses to add it's extra controls just above the keyboard when editing a form.
PeyloW
CORRECTION: You should use `inputAccessoryView` to add a view above the default keyboard. `inputView`is for replacing the default keyboard all together.
PeyloW
Hey that's COOOOOOOOOOL man, haven't ever heard of inputAccessoryView!!! That's my favorite UIKit feature now! COOL!
BugAlert