UISearchcontroller搜索栏委托不起作用 [英] UISearchcontroller searchbar delegate not working

查看:68
本文介绍了UISearchcontroller搜索栏委托不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, UISearchBarDelegate {

我包括了搜索栏委托

searchController?.searchBar.delegate = self

我已经设置了代表。

但是方法

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

不会发生。

Delegate设置正确,并且 stopHighlight()是实函数

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

推荐答案

使用 viewdidload 方法使 searchbar 的委托。 / p>

Make delegate of searchbar in viewdidload method.

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

,也可以在情节提要中将其设置为

or you can set it in storyboard as

-因此,代表将呼叫为

 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
    }

这篇关于UISearchcontroller搜索栏委托不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆