How do I dismiss a UITextView using the Done keyboard button?

aherlambang picture aherlambang · Jan 31, 2011 · Viewed 22k times · Source

Possible Duplicate:
How to dismiss keyboard for UITextView with return key?

I have a UITextView and have done the following:

text.returnKeyType = UIReturnKeyDone;

so that I can dismiss the keyboard when I press on done, however when i click on Done all it does is to go into the next line. How can I change this behavior? Thanks for the help

Answer

zoul picture zoul · Jan 31, 2011

There’s no shouldReturn on UITextView, but you can implement UITextViewDelegate and watch for insertions:

- (BOOL) textView: (UITextView*) textView
    shouldChangeTextInRange: (NSRange) range
    replacementText: (NSString*) text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

Edit: And yes, sorry, this is a dupe.