Uisearch结果更新协议未接收到任何数据

时间:2019-01-14 18:23:55

标签: ios swift uisearchcontroller

当我开始在搜索栏上键入内容时,我的协议没有收到任何数据。我的目的是在搜索控制器的搜索栏上输入开始类型时获取数据。

代码:

class FirstViewController:UIViewController,UISearchResultsUpdating{
    func updateSearchResults(for searchController: UISearchController) {
        self.labels.text="sd"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let searchcontrol=UISearchController(searchResultsController:self)

        searchcontrol.searchResultsUpdater=self
        let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))

        subView.addSubview((searchcontrol.searchBar))
        searchcontrol.searchBar.sizeToFit()
        searchcontrol.hidesNavigationBarDuringPresentation = false
        view.addSubview(subView)
    }
}

1 个答案:

答案 0 :(得分:0)

如rmaddy所说,尝试这样的事情:

class FirstViewController: UIViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        searchcontrol.searchResultsUpdater=self
        let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))

        subView.addSubview((searchcontrol.searchBar))
        searchcontrol.searchBar.sizeToFit()
        searchcontrol.hidesNavigationBarDuringPresentation = false
        view.addSubview(subView)
    }

}

extension FirstViewController: UISearchResultsUpdating{

    func updateSearchResults(for searchController: UISearchController) {
        self.labels.text="sd"
    }

}