iPhone: Hide Keyboard on App Did Enter Background or View Did Disappear

Dave picture Dave · Aug 18, 2012 · Viewed 8.9k times · Source

I have a UISearchBar which when clicked, shows the keyboard. However if the user presses the home button while the keyboard is shown and then goes back into the app, the keyboard is still visible. How can I hide th keyboard when the app closes/enters background?

I've tried the following in viewDidDisappear:

[eventSearchBar resignFirstResponder];

[eventSearchBar endEditing:YES];

I've also tried this in the delegate in appDidEnterBackground:

[self.rootController.navigationController.view endEditing:YES];

None of these worked.

Answer

Rajneesh071 picture Rajneesh071 · Aug 18, 2012

you can do this in appDelegate....

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.window endEditing:YES];
}