1

I would like to post a more specific question to this one.

I implemented a custom UISearchBar, and thus a custom UISearchController, as this seemed to be the only solution to stop the search bar from displaying a Cancel button. This indeed solved the mentioned problem, but created a new one. For some reason, the updateSearchResultsForSearchController: delegate method (of the viewController where I have implemented the searchController) doesn't get called anymore (it does the first time I tap on the searchBar, but doesn't when the search string is being typed), while it does with a standard UISearchController and UISearchBar. As a result, the search results table fails to appear altogether.

Here is my code for SearchBar.h

@interface SearchBar : UISearchBar

@end

and SearchBar.m

#import "SearchBar.h"

@implementation SearchBar

- (void)setShowsCancelButton:(BOOL)showsCancelButton {
    // Do nothing...
}

- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
    // Do nothing....
}

@end

as well as SearchController.h

@interface SearchController : UISearchController

@end

and SearchController.m

#import "SearchController.h"
#import "SearchBar.h"

@interface SearchController () {
SearchBar *_searchBar;
}
@end

@implementation SearchController

- (void)viewDidLoad {

    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
}

- (SearchBar *)searchBar {

    if (_searchBar == nil) {
        _searchBar = [[SearchBar alloc] initWithFrame:CGRectZero];
    }
    return _searchBar;
}

@end

In MyViewController.h I subscribe to the following protocols:

@interface MyViewController : UIViewController <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>

Can anyone see what I may have missed? Thanks in advance.

Community
  • 1
  • 1
artooras
  • 6,315
  • 9
  • 45
  • 78
  • 1
    See [this answer](http://stackoverflow.com/a/28396967/3985749). I think you need to ensure that the searchBar activates the Search Controller, perhaps in the `textDidChange:` searchBar delegate method. – pbasdf Jul 06 '15 at 21:48
  • If the only thing you want to do is to get rid of the “Cancel” button, why didn’t you just set the corresponding property on the `UISearchBar`? – Rafael Bugajewski Jul 09 '15 at 11:53
  • Thanks @pbasdf, setting `self.searchController.active = YES` did it for me. However, differently from your linked answer, I set `MyViewController ` as the `searchBar.delegate` as I needed some other delegate performance in my view controller as well. – artooras Jul 13 '15 at 13:00
  • @Rafael, unfortunately, the property seems to do nothing, hence the workaround.. – artooras Jul 13 '15 at 13:00

0 Answers0