简体   繁体   中英

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

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

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

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];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM