2

I want to resign keyboard from UItextview. How to implement UItextView delegate method programmatically.

gauri
  • 71
  • 3

4 Answers4

6

If u want that ur keyboard is resigned when click on return then u have to write, implement this method....

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

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

return YES;

}

Just make it copy and paste....:)

Krunal
  • 1,318
  • 1
  • 13
  • 31
5

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;
}
Emil
  • 7,220
  • 17
  • 76
  • 135
anjum
  • 556
  • 4
  • 22
4

You should use the UITextViewDelegate. You have to declare the use of the protocol in your class header like:

@interface YourClass:NSObject<UITextViewDelegate>

Then in your .m, you should set your class as delegate in some point with something like:

textView.delegate = self;

Then, in your .m again, you have to implement the delegate methods, in particular:

textViewDidChange:

You can read the protocol reference at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html for more information.

LuisEspinoza
  • 8,508
  • 6
  • 35
  • 57
4

There is no specific delegate method for UITextview to know when user hits "RETURN" So you can do like this

//In .h File

@interface BlahBlah : UIViewController <UITextViewDelegate>
  @property(nonatomic, retain) IBOutlet UITextView *myTextView;
@end

//In .m File
@implementation BlahBlah

@synthesis myTextView;

//In some method, can be viewDidLoad OR viewDidAppear . your convenience ;) 
{
   self.myTextView.delegate = self;
}

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

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

        return YES;
}
Krrish
  • 2,256
  • 18
  • 21