9

My keyboard appears with a textView, I want to hide it when the user push on a back button on a navigation bar.

I have tried this:

-(void)viewWillDisappear:(BOOL)animated{

    [myTextView resignFirstResponder];
}

and this:

-(void)viewDidDisappear:(BOOL)animated{

    [myTextView resignFirstResponder];
}

But it doesn't work, how can I do this?

edit:

I found the solution here:

iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet

Community
  • 1
  • 1
Anthony
  • 2,801
  • 3
  • 30
  • 49
  • make sure IBoutlets are binded correctly and viewDid/WillDisappear are being called by putting NSLog statements – Ayaz Alavi Apr 19 '12 at 12:59
  • You shouldn't even have to resign first responder yourself. When the view comes off the screen it should automatically resign and have the keyboard vanish. You can test this behaviour by creating a new project for the iphone with the "Master-Detail Application" template. Open the storyboard and go to the detail screen. Add a `UITextField` to the view and run the app. When you you tap in the field, keyboard appears and when you hit the back button on the nav bar it vanishes. – DBD Apr 19 '12 at 13:03
  • Is true but, but the case arrive when I push on a button, a modal form sheet appears with an UINavigationController, I push on another button that navigate in another view, I push in a textView, the keyboard appears, I go back in the previous viewController and the keybord is still present. – Anthony Apr 23 '12 at 08:06
  • 1
    I have found the solution here: http://stackoverflow.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui – Anthony Apr 23 '12 at 08:20

2 Answers2

17

Put this into the buttonPress method -

[self.view.window endEditing:YES];

Edit - this also lets you get the contents of the text being edited when the "back" button is pressed

SomaMan
  • 4,127
  • 1
  • 34
  • 45
3

Combining the above answers and checking for back button will be done by this

- (void)viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
    // back button was pressed.  We know this is true because self is no longer
    // in the navigation stack.
    [self.view.window endEditing:YES];
}

[super viewWillDisappear:animated];

}

megha
  • 473
  • 5
  • 18