UISearchcontroller搜索栏委托不工作

时间:2017-04-30 13:07:17

标签: ios swift delegates uisearchcontroller

class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, UISearchBarDelegate {

我已添加了搜索栏代理

searchController?.searchBar.delegate = self

我已经设置了委托。

但方法

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

不会发生。

委托已正确设置且stopHighlight()是一个真正的功能

2 个答案:

答案 0 :(得分:2)

searchbar方法中委派viewdidload

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

或者您可以在故事板中将其设置为 enter image description here

- 代表将打电话给

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

修改: 对于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
    }

答案 1 :(得分:-1)

就我而言,这是拼写错误。

确保您的@IBOutlet名称与您的委托人名称相似。

例如, @IBOutlet弱变量 SearchBar :UISearchBar!

在viewDidLoad()中设置委托:

override func viewDidLoad() {
    super.viewDidLoad()
    SearchBar.delegate = self