Uisearchcontroller Searchbar Delegate Not Working

UISearchcontroller searchbar delegate not working

Make delegate of searchbar in viewdidload method.

 override func viewDidLoad() {
super.viewDidLoad()
searchBarCustom.delegate = self
}

or you can set it in storyboard as
Sample Image

-hence delegates will call as

 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("searchBarCancelButtonClicked")
}

EDIT:
For UISearchController

var searchController: UISearchController!
func configureSearchController() {
// Initialize and perform a minimum configuration to the search controller.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()

// Place the search bar view to the tableview headerview.
tblSearchResults.tableHeaderView = searchController.searchBar
}

Why are my UISearchController delegate methods not being called?

You have a property named searchController. But then you create a local variable of the same name in your configureSearchController method. So in the end you never set the searchController property. As a result, your locally created search controller goes out of scope, has no more references, and gets deallocated.

The fix is to update your code a little.

UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.placeholder = @"Search for friends...";
searchController.delegate = self;
searchController.searchBar.delegate = self;
[searchController.searchBar sizeToFit];

[searchController.searchBar setBarStyle:UIBarStyleBlack];
[searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
searchController.searchBar.tintColor = [GBColor accentGray];
searchController.searchBar.backgroundColor = [GBColor darkGray];
searchController.searchBar.barTintColor = [GBColor accentGray];
searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);

self.searchController = searchController;

Unable to add UISearchController's search bar into navigationbar and delegate methods not getting called

Just define UISearchController as property then everything working fine.

I have tested below in the sample project.

  class ViewControllerName: UIViewController{
let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()
let search = UISearchController(searchResultsController: nil)
self.navigationItem.searchController = search
self.navigationItem.searchController!.searchBar.delegate = self
self.navigationItem.searchController!.searchResultsUpdater = self
self.navigationItem.searchController?.searchBar.showsCancelButton = true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
print("Called")
}

func updateSearchResults(for searchController: UISearchController) {
print("Called")
}
}

Hope it will work for you.

Search bar cancel button delegate not called

The function delegate is not being called because you are missing the definesPresentationContext:

Determines which parent view controller's view should be presented over for presentations of type
UIModalPresentationCurrentContext. If no ancestor view controller has this flag set, then the presenter
will be the root view controller.

you may enable such flag in this way:

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.searchController = UISearchController(searchResultsController: nil)
self.navigationItem.searchController?.searchBar.delegate = self
self.definesPresentationContext = true
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("searchBarCancelButtonClicked")
}
}

Note:
without definesPresentationContext, you are not really touching the cancel button (when you try to touch it), you are just dismissing the context (you may notice a "silent" glitch in the background focus), suchlike
a popover is being dismissed.

UISeachController Delegate methods not called, searchbar can't become first responder

The problem is you are trying to access UI element(searchbarcontroller) before UI is completely loaded.
This can be done in 2 ways

  1. Use main queue to show keypad

    private func setupSearchController() {
    self.navigationItem.searchController = searchController
    searchController.definesPresentationContext = true
    searchController.delegate = self
    searchController.isActive = true
    searchController.searchBar.delegate = self
    DispatchQueue.main.async {
    self.searchController.searchBar.becomeFirstResponder()
    }
    }

With this approach keypad will be only show at one time in viewDidLoad


  1. Show keypad in viewDidAppear

     override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    DispatchQueue.main.async {
    self.searchController.searchBar.becomeFirstResponder()
    }
    }

With this approach keypad will be always shown whenever screen appears.

UISearchController does not trigger delegate methods and search bar delegate methods

searchController.active = YES;

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



Related Topics



Leave a reply



Submit