单击“取消"按钮如何退出搜索? [英] How to exit from the search on clicking on Cancel button?

查看:121
本文介绍了单击“取消"按钮如何退出搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a search bar with cancel button. But when I click on Cancel button it doesn't close the search bar. How can I make that on click on Cancel it will return search bar to the first state?

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

If you have any questions - ask me, please

推荐答案

您需要实现UISearchBarDelegate:

You need to implement the UISearchBarDelegate :

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

将搜索栏委托设置为self

Set the search bar delegate to self

 override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.showsCancelButton = true
        searchBar.delegate = self

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

and then implement the butCancelSearchAction delegate function to do whatever you need to do to cancel the search action and reset the search text:

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 
}

这篇关于单击“取消"按钮如何退出搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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