Question

So i am making an app and am having some problems with dismissing the keyboard from UISearchBar and UITextFields. Here is the structure of my app:

NavigationController -> ViewC1 - (Modally)-> ViewC2 -(Modally) -> ViewC3

I have a search box in ViewC1, and when the "Search" button on the keyboard is pressed the keyboard is dismissed, this works fine. However if i return to ViewC1 after being in ViewC3 the keyboard no longer dismisses when the "Search" button is pressed. In the search bar delegate method i have put as follows:

- (void) searchBarSearchButtonClicked:(UISearchBar *)search
{
if ([search isFirstResponder]) {
    [search resignFirstResponder];
  } else {
    [search becomeFirstResponder];
    [search resignFirstResponder];
  }
}

This does not solve the problem and i am not sure why the keyboard is not dismissing. For reference, when returning to the original ViewC1, ViewC3 is dismissed as follows:

UIViewController *parent = self.presentingViewController;
[parent.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Any help is appreciated, thanks.

Was it helpful?

Solution

Okay i figured out what the problem was. They first responder was being resigned but the keyboard was not disappearing because of a focus issue. There is a default behaviour on modal views to not dismiss the keyboard (which is not a bug apparently). So after returning from the modal view it was still having this behaviour (resigning first responder but not dismissing keyboard). The way i solved this was by placing the following code in both the modal views .m files:

- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}

This solved it for me. Then by either using:

[search resignFirstResponder];

or

[self.view endEditing: YES];

The keyboard will dismiss fine!

OTHER TIPS

You'll need to do some debugging with break points to figure out why that conditional statement is not being hit. You could also use the endEditing method in UIView to simply resign the responder whenever search is clicked:

- (void) searchBarSearchButtonClicked:(UISearchBar *)search
        [search endEditing:YES];
}

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

Try it....

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   [mySearchBar resignFirstResponder];
}

Please declare IBOutlet UISearchBar *mySearchBar; in your .h file
Set delegate in your .xib file.

Hope this helped

(A year later..)

I've just had the same problem with my iPad app.

I had a"Please register" UIView containing a few UITextFields which I would pop onto the screen. When the user tapped on a Close button, it'd disappear, and I'd use removeFromParentViewController to get rid of it.

[self.pleaseRegisterDlg removeFromParentViewController];

Now, when I ran this code on a real device in debugging mode from XCode, the story ended there. It all worked fine. But when I built an In-House app with this code, it behaved differently.

I would find that sometimes, no matter how many resignFirstResponders or disablesAutomaticKeyboardDismissals I put into the code, there would be times when the onscreen keyboard would suddenly appear, and refuse to go away programatically.

It made no sense, as the rest of my app didn't have any UITextFields... there was no reason for the app to display a keyboard.

My solution was to set the "Please Register" UIView to nil after removing it from the parent view.

[self.pleaseRegisterDlg removeFromParentViewController];
pleaseRegisterDlg = nil;

Apparently, having a UIView which isn't actually attached to any other UIViews but which contains UITextFields is sometimes enough to confuse iOS, and make the onscreen keyboard appear.

(Sigh. This one line of code wasted a few hours of my afternoon.. lesson learned !)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top