0

I have a UIPopoverController with UITableViewController in it. Also, I am using UISearchDisplayController with it. My class interface is like this:

@interface SearchController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UIPopoverControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate> {

UISearchBar *_searchBar;
UISearchDisplayController *_searchDisplayVC;
}

My init looks like this:

//create a search bar
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchBar.delegate = self;

_searchDisplayVC = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];

self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;

// add a searchbar
self.tableView.tableHeaderView = _searchBar;

My issue is that when I select search bar to type search phrase in it, the keyboard does not dismiss even if trying this:

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    return YES;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    NSLog(@"Dismiss popover controller");
    [_searchBar becomeFirstResponder];
    [_searchBar resignFirstResponder];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    //trying to resolve the issue with not working automatic hiding of a keyboard.
    [searchBar resignFirstResponder];
    [searchBar endEditing:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [TableSearchViewController dismissKeyboard];
}

-(BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

+ (void)dismissKeyboard {
    [[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
}

Anyone knows how to dismiss the keyboard?

piotr_ch
  • 508
  • 3
  • 7
  • 17
  • In which situation you can't dismiss the keyboard? Is it when you clicked the search button? – ujell May 27 '14 at 14:23
  • Exactly, when I tap the searchbox textfield then any of the usual methods work. – piotr_ch May 28 '14 at 06:49
  • Problem is probably from `dismissKeyboard` method. Check it again and if you can't solve it edit your post and add that method. – ujell May 28 '14 at 06:54
  • sorry I forgot to add this method implementation. Post is updated with `dismissKeyboard` method. – piotr_ch May 28 '14 at 08:25
  • Can you take a breakpoint in the `dismissKeyboard` method and be sure if it is called? Also why don't you just call `[searchBar resignFirstResponder];` instead `dismissKeyboard`? – ujell May 28 '14 at 08:40

1 Answers1

2

I have found a resolution to this issue. In parent view controller I have overrided the UIViewController method named disablesAutomaticKeyboardDismissal to that:

-(BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

And It works fine! :) The keyboard is hiding as expected. Thanks @ujell for interesting in to this issue:)

piotr_ch
  • 508
  • 3
  • 7
  • 17