Dvole
Dvole

Reputation: 5795

UISearchController does not trigger delegate methods and search bar delegate methods

So I have UISearchController, it shows the bar, I can enter text but it does not trigger delegate methods. Here is my code:

@IBAction func searchTapped(AnyObject) {

    NSLog("search...")

    let cancelButton = UIBarButtonItem(image: UIImage(named: "delete_sign-50"), landscapeImagePhone: nil, style: .Plain, target: self, action: "cancelTapped:")

    var searchController = UISearchController(searchResultsController: nil)
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "enter the text to search"
    searchController.searchResultsUpdater = self;
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.delegate = self
    searchController.searchBar.delegate = self

    self.definesPresentationContext = true;


    UIView.animateWithDuration(0.2, animations: { () -> Void in

        self.navigationItem.rightBarButtonItems = nil
        self.navigationItem.rightBarButtonItems = [cancelButton]
        self.navigationItem.titleView = searchController.searchBar
        searchController.searchBar.sizeToFit()
        self.toolbar.hidden = false
        self.tableView?.hidden = false
    })

    //add first responser to search bar
    searchController.searchBar.becomeFirstResponder()

}

func updateSearchResultsForSearchController(searchController: UISearchController) {

    NSLog("1 %@", searchController.searchBar.text);

}

func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    NSLog("2 %@", text)
    return true
}

None of these methods is getting called when text changes.

More than that, I tried adding searchController as variable to my controller, and in that case it works bonkers - when I tap the search bar it moves off screen.

Upvotes: 0

Views: 3549

Answers (2)

dip
dip

Reputation: 139

For IOS 9

    -(void )viewDidLoad
 _searchController = [[UISearchController alloc] initWithSearchResultsController:self];
    self.navigationItem.titleView  = _searchController.searchBar;
    self.definesPresentationContext = YES;
    _searchController.delegate = self;
    _searchController.searchBar.delegate = self;
    _searchController.searchBar.placeholder = NSLocalizedString(@"SEARCHTITLE", nil);
    [_searchController.searchBar setShowsCancelButton: YES animated: NO];

Upvotes: 0

Dvole
Dvole

Reputation: 5795

searchController.active = YES;

This helped, not sure why it is not mentioned anywhere.

Upvotes: 3

Related Questions