如何在单击“取消”按钮时退出搜索?

时间:2016-01-09 10:22:21

标签: ios swift uisearchbar

我有一个带取消按钮的搜索栏。但是当我点击取消按钮时,它不会关闭搜索栏。如何点击取消它会将搜索栏返回到第一个状态?

如果您有任何疑问 - 请问我

10 个答案:

答案 0 :(得分:44)

您需要实现UISearchBarDelegate:

class ViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var searchBar: UISearchBar!

将搜索栏委托设置为self

 override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.showsCancelButton = true
        searchBar.delegate = self

然后实现butCancelSearchAction委托函数,以执行取消搜索操作和重置搜索文本所需的操作:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    // Do some search stuff
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    // Stop doing the search stuff
    // and clear the text in the search bar
    searchBar.text = ""
    // Hide the cancel button
    searchBar.showsCancelButton = false
    // You could also change the position, frame etc of the searchBar 
}

答案 1 :(得分:10)

class MyController: UIViewController, UISearchBarDelegate {
    // Called when search bar obtains focus.  I.e., user taps 
    // on the search bar to enter text.
    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchBar.showsCancelButton = true
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchBar.text = nil
        searchBar.showsCancelButton = false

        // Remove focus from the search bar.
        searchBar.endEditing(true)

        // Perform any necessary work.  E.g., repopulating a table view
        // if the search bar performs filtering. 
    }
}

答案 2 :(得分:6)

尝试。它工作正常。

class MyViewController: UIViewController, UISearchBarDelegate {

    func searchBarTextDidBeginEditing(_searchBar: UISearchBar) {
        searchBar.setShowsCancelButton(true, animated: true)
        //write other necessary statements
    }

    func searchBarCancelButtonClicked(_searchBar: UISearchBar) {
        searchBar.text = nil
        searchBar.setShowsCancelButton(false, animated: true)

        // Remove focus from the search bar.
        searchBar.endEditing(true) 
    }
} 

答案 3 :(得分:3)

试试这个:

searchController.active = false

答案 4 :(得分:2)

我也遇到过这个问题,在我的情况下,从表格视图中搜索任何项目后,单击其中一个结果项目打开的详细信息视图,但搜索栏不会消失。我使用Nicat's answer进行了一些更改,这是我的版本:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    searchController.isActive = false
    self.searchBarCancelButtonClicked(searchController.searchBar)

    ...
}

答案 5 :(得分:2)

Swift 3

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsCancelButton = true
    return true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    self.searchBar.endEditing(true)
    searchBar.resignFirstResponder()
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    self.searchBar.endEditing(true)
    searchBar.showsCancelButton = false
    searchBar.resignFirstResponder()
}

The Cancel button appears only when the user start entering characters in the field.

答案 6 :(得分:2)

非常快速和简单。只需符合UISearchBarDelegate协议,然后在您的类中实现此功能:

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}

resign表示会退出

FirstResponder是视图的焦点

总之,这会使焦点远离搜索栏,从而有效地取消它。

答案 7 :(得分:1)

您需要实施 UISearchBarDelegate

UISearchBarDelegate协议定义了为实现UISearchBar控制功能而实现的可选方法。 UISearchBar对象为条形图上的搜索字段提供用户界面,但应用程序负责在按下按钮时实施操作。在delegate中输入文字时,textField至少需要执行实际搜索。 Read this

在目标C

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

     [self.searchDisplayController setActive:NO animated:YES];

}

在Swift中:

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    self.searchDisplayController.setActive(false, animated: true)
}

答案 8 :(得分:0)

当您点按取消按钮时,searchBar会看到您有新搜索的内容,这就是为什么您将resignFirstResponder()放在{{1}上点击它不会起作用,因为在此之后它会调用cancelButton

所以我在didBeginText中添加了一个条件,所以当搜索中的字符串少于或少于0个字符时,它将重新签名第一个响应者。就像这样:

didBeginText

这是一个简单的方法,也可以。 最好的问候!

答案 9 :(得分:0)

对于Objective-C解决方案

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    if(@available(iOS 11.0, *)){
        [self.navigationController.navigationItem.searchController.searchBar resignFirstResponder];
    }
    // reload your table or whatever you want.
    // searchController is available after the iOS 11, so it will be good to use @available check
}