如何让UISearchController等待点击搜索

Sly*_*oth 2 ios swift uisearchcontroller

我正在尝试在我的SWIFT代码中实现搜索。我希望它等到单击搜索按钮后再执行任何操作。到目前为止我所拥有的。

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    self.filteredCats.removeAll(keepCapacity: false)
    filteredCats = art.filter{
        $0.sarticleTitle.lowercaseString.rangeOfString(searchController.searchBar.text!.lowercaseString) != nil
    }
        print(searchController.searchBar.text)
        self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)

用户输入内容后,它将打印出所有信息。我希望它一直等到单击搜索按钮或用户完成所有输入为止。

小智 5

您应该像这样将委托设置为视图控制器。

var resultSearchController = UISearchController()
resultSearchController.searchBar.delegate = self

extension ViewController: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        print("search button click")
    } 
}
Run Code Online (Sandbox Code Playgroud)