How to get the Return key to dismiss a UITextView

Need to dismiss they keyboard in a UITextView? Changing the Return key to read “Done” doesn’t make the iPhone keyboard to go away.  Put this code in the delegate for your UITextView and your return key can behave more like the Done button in the keyboard for a UITextField.

#pragma mark -
#pragma mark UITextViewDelegate methods

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

This entry was posted in iPhone Development, Programming and tagged , , . Bookmark the permalink.

3 Responses to How to get the Return key to dismiss a UITextView

  1. ersin says:

    this code dosent work…

  2. Jeevan Kumar says:

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

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

    return YES;
    }

    This should work

  3. suneel says:

    Yaa its working Thanks alot

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.