views:

9433

answers:

6

In IB's library, the introduction tells us that when the return key is pressed, the keyboard for UITextView will disappear. But actually the return key can only act as '\n'.

I can add a button and use [txtView resignFirstResponder] to hide the keyboard. But is there a way to add the action for the return key in keyboard so that I needn't add another button.

A: 

Connect an action to the "Did End On Exit" event in IB.

Brent Royal-Gordon
Thanks, I didn't notice this event before. For UITextField, -(BOOL)textFieldShouldReturn:(UITextField *)textField can also be used to dismiss keyboard.
iPhoney
Did End on Exit does not exist for a UITextView. Thats a UITextField action
lostInTransit
+10  A: 

UITextView does not have any methods which will be called when the user hits the return key. If you want the user to be able to add only one line of text, use a UITextField. Hitting the return and hiding the keyboard for a UITextView does not follow the interface guidelines.

Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.

There might be other ways but I am not aware of any.

lostInTransit
Thanks and the method works well. The reason to use UITextView is that it can hold text in multiple lines. And now I'm using it as a message box.
iPhoney
It is easy enough to change the return key to "done" using either `[textField setReturnKeyType: UIReturnKeyDone];` or using interface builder
Casebash
Okay, I now understand that the Apple way of finishing with a multiline text field is to add done to the menu bar
Casebash
+3  A: 

Follow instructions on this blog post:

http://iphonedevelopertips.com/cocoa/how-to-dismiss-the-keyboard-when-using-a-uitextview.html

mjnagpal
thanks. helped very much..
Sijo
A: 

still looking for dissmissal of keyboard on textView

regards shishir

shishir.bobby
@ Sam Vthat snippet was cool man.it works for me.thanks a ton man.Is there any snippet for dissmisal of numeric keyboard.regardsshishir
shishir.bobby
Please do not post comments as answers
Casebash
+12  A: 

Figured I would post the snippet right here instead:

Make sure you declare support for the UITextViewDelegate protocol.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}
Sam V
A: 

Thank you very much. I liked the aproach. :) Gaurav

Gaurav
Please do not post comments as answers
Casebash