UISearchcontroller searchbar delegate not working

↘锁芯ラ 提交于 2019-12-22 11:24:31

问题


class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, UISearchBarDelegate {

I have included searchbar delegate

searchController?.searchBar.delegate = self

and I have set the delegate.

But method

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    stopHighlight()
}

doesn't happen.

Delegate has set properly and stopHighlight() is a real function


回答1:


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
    }



回答2:


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


来源:https://stackoverflow.com/questions/43706731/uisearchcontroller-searchbar-delegate-not-working

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