5

hi i am working on UITextview

how to Resign Keyboard, after keyboard "Done button" click Action, which XIB

Thank you

good guy
  • 559
  • 5
  • 11
  • 25
  • - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange: (NSRange)range replacementText:(NSString *)text { // Any new character added is passed in as the "text" parameter if([text isEqualToString:@"\n"]) { // Be sure to test for equality using the "isEqualToString" message [textView resignFirstResponder]; // Return FALSE so that the final '\n' character doesn't get added return NO; } // For any other character return TRUE so that the text gets added to the view return YES; } //CHECK THIS IT WORKS FOR YOU – good guy Jan 31 '11 at 11:16
  • 2
    if your comment is the solution you've ended with, you should instead post it as an answer and mark that, otherwise pick one of the answers below. I vote for @Robotic_Cat because his solution worked for me. However, @WrightCS's solution is also good for more fine-grain control. – Gup3rSuR4c Jan 31 '11 at 23:20
  • Thank you alex please check my answer below – good guy Feb 01 '11 at 09:46

4 Answers4

11

hi if you want answer for, Resign the keyboard of UITextview with default "Done" button on keyboard, then this is answer for that

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

    // Any new character added is passed in as the "text" parameter

    if([text isEqualToString:@"\n"]) {

        // Be sure to test for equality using the "isEqualToString" message

        [textView resignFirstResponder];

        // Return FALSE so that the final '\n' character doesn't get added

        return NO;
    }

    // For any other character return TRUE so that the text gets added to the view

    return YES;
}

i followed this to resign keyboard of UITextview, if there is any concern inform me

Thank You

highlycaffeinated
  • 19,221
  • 7
  • 56
  • 90
good guy
  • 559
  • 5
  • 11
  • 25
  • for UITextField use this instead: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)text{ if([text isEqualToString:@"\n"]) { [textField resignFirstResponder]; return NO; } return YES; } – avance May 22 '12 at 00:21
9

Create an IBAction and in IB hook it up to the UITextfield's DidEndOnExit event. Then call the [textViewName resignFirstResponder] to make the keyboard go away.

Alternatively, try using [self.view endEditing:YES] in the IBAction.

Hemang
  • 25,740
  • 17
  • 113
  • 171
Robotic Cat
  • 5,861
  • 4
  • 39
  • 57
  • This is actually the only answer for the original poster's actual question although the other methods will also dismiss the keyboard. The original poster asked for a way in the nib to connect an action to be executed when the 'done' button is pressed. This answer explains how to do that. – Logachu Feb 25 '13 at 17:49
  • This seems a better solution than the one accepted as an answer, which is checking every character input – morksinaanab Apr 23 '13 at 02:36
4

To assign the action to the Done button of a keyboard:

In these examples, myTextView is a UITextView that is defined in the header and connected in Interface Builder.

-(BOOL)textViewShouldReturn:(UITextView*)textView {
    if (textView == myTextView) {
        [textView resignFirstResponder];
    }
    return YES;
}

To assign it to an external button:

Be sure to define an IBAction in your header, then in Interface Builder, connect the button to this action for touchUpInside:

.h

-(IBAction)dismissKeyboard:(id)sender;

.m

-(IBAction)dismissKeyboard:(id)sender {

    [myTextView resignFirstResponder];

}
WrightsCS
  • 49,871
  • 22
  • 132
  • 180
  • 1
    There is no such delegate method:-(BOOL)textViewShouldReturn:(UITextView*)textView – Sofi Software LLC Jan 30 '12 at 22:03
  • 1
    I must correct you. There is such a method and WrightCS is correct here as you can see in this other post as well. http://stackoverflow.com/questions/389825/dismiss-iphone-keyboard – FreeAppl3 Jul 13 '12 at 08:53
  • 1
    No there isn't. There is a `textFieldShouldReturn` method for `UITextField`, but no similar method for `UITextView`. Please check your facts. – Jean-Denis Muys Feb 07 '13 at 10:09
0

Call resignFirstResponder on the textview that has focus in order to dismiss the keyboard.

Brian
  • 15,450
  • 4
  • 44
  • 62