How to hide keyboard after a push on an UINavigationBar back button on ios

Anthony picture Anthony · Apr 19, 2012 · Viewed 7.1k times · Source

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

Answer

SomaMan picture SomaMan · Apr 19, 2012

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