UISearchController无法将委托设置为self

时间:2017-01-24 05:37:39

标签: ios uiviewcontroller swift3 uisearchcontroller

我正在尝试使用UISearchController

在我的导航栏中使用swift3

我目前拥有它,因此右上角有一个搜索图标。当您点击图标时,它会显示UISearchController

问题是,当我尝试searchController.delegate = self时,我收到错误

  

不能指定'ParentViewController'类型的值来输入'UISearchControllerDelegate'吗?

我假设因为我无法设置委托,所有动作都没有链接,但我无法弄清楚为什么它不会让我?

这是我想要的代码。我是否实施了错误的协议,只是没有看到它,或完全错过了什么?

class ParentViewController: ButtonBarPagerTabStripViewController, UISearchResultsUpdating, UISearchBarDelegate {

var searchBarButtonItem: UIBarButtonItem?

override func viewDidLoad() {
    searchBarButtonItem = navigationItem.rightBarButtonItem
    super.viewDidLoad()
}

func updateSearchResults(for searchController: UISearchController) {
    print("update search results")
}

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

func filterContentForSearchText(searchText: String, scope: String = "All") {
    print("Filter: " + searchText)
}


@IBAction func startSearchAction(sender: UIBarButtonItem) {
    print("Clicked")

    let searchController = UISearchController(searchResultsController: nil)

    searchController.delegate = self
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.showsCancelButton = true
    searchController.dimsBackgroundDuringPresentation = true

    self.navigationItem.titleView = searchController.searchBar

    definesPresentationContext = true

    navigationItem.setRightBarButton(nil, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

0 个答案:

没有答案