UISearchcontroller searchbar delegate not working

走远了吗. 提交于 2019-12-06 10:59:50

Make delegate of searchbar in viewdidload method.

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

or you can set it in storyboard as

-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
    }

In my case, it was a spelling error.

Make sure your @IBOutlet name is similar with your delegate name.

Example, @IBOutlet weak var SearchBar: UISearchBar!

Set the delegate in viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    SearchBar.delegate = self
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!