2

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.

sj.cleaver
  • 115
  • 2
  • 11
  • I recommend debugging your search button click method. Is the search bar and its delegate still allocated? Also is search recognized as first responder at this point? – Ben M Apr 08 '13 at 15:24
  • @Ben M: It is still being recognised as the firstResponder and therefore it does still have its delegate allocated as it is triggering the delegate method. – sj.cleaver Apr 13 '13 at 09:43

4 Answers4

4

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!

sj.cleaver
  • 115
  • 2
  • 11
0

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

ahtierney
  • 318
  • 1
  • 8
  • I added a log statement within the if else statement and after returning from ViewC3 it is claiming that it is the first responder, however is still not dismissing the keyboard? I also tried your suggestion of [search endEditing:YES] and it has exactly the same behaviour. Works first time round but after returning from ViewC3 it does not dismiss the keyboard. – sj.cleaver Apr 08 '13 at 16:08
  • so to clarify, your debugger shows that your search bar is the first responder, you've placed a break point on the line inside of your conditional that resigns the frist responder if it is your search bar, that line is executed and the first responder is still your search bar? – ahtierney Apr 08 '13 at 17:14
  • After debugging i can confirm that the if statement is entered both times (first time round and after returning from ViewC3) indicating it is the first responder, and then resigns the first responder. It acts the same both times round but for some reason the keyboard is still not being dismissed – sj.cleaver Apr 08 '13 at 17:30
0

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

Chirag Pipaliya
  • 1,281
  • 12
  • 20
  • This is how it was all already set up. The search bar is declared as an IBOutlet in the .h file, its delegate is set in the viewDidLoad method of the .m file. And the search bar delegate method is set up as shown in my question. – sj.cleaver Apr 11 '13 at 15:14
  • @sj.cleaver : Please set delegate in .xib – Chirag Pipaliya Apr 12 '13 at 04:07
  • I tried setting the delegate in the storyboard (not using .xib) and still having exactly the same problem – sj.cleaver Apr 13 '13 at 09:44
  • @sj.cleaver : Set in .h file – Chirag Pipaliya Apr 13 '13 at 10:17
  • @sj.cleaver : Set breakpoints and check that your function is called or not. tell me what happen. – Chirag Pipaliya Apr 13 '13 at 11:26
  • @ Chirag Pipaliya : Everything is set up as stated and i have checked that the function is called it it is all called fine. It does actually resign the first responder, i can tell this because the keyboard becomes inactive, typing anything will not write it in the search bar, but it does not dismiss. I believe it may be some sort of focus problem, it is not refocusing after returning from the modal view and therefore the keyboard is staying up. – sj.cleaver Apr 13 '13 at 12:29
0

(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 !)

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159