ios - swift iOS9 : How to recognize a click on the X button of the UISearchBar

标签 ios swift uisearchbar

当用户清除他正在搜索的词时,我必须更改 bool 变量值。他在点击搜索栏右侧的 X 图标时将其清除。

我不知道如何获得这个 Action 。搜索栏相关函数只识别取消按钮、书签按钮和搜索结果按钮的点击。

我如何在 iOS9 设备上使用 Swift 2 来做到这一点?

最佳答案

您可以实现 UISearchBarDelegate 的委托(delegate)函数:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  if  searchText.count == 0 {
    //call your func
  }
}

如文档所述:

Discussion
This method is also invoked when text is cleared from the search text field.

关于ios - swift iOS9 : How to recognize a click on the X button of the UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33700762/

相关文章:

iphone - 如何使用 iphone 计算我们汽车的速度

ios - 解压错误 操作无法完成。 (Zip.ZipError 错误 1。)ViewController 之间

swift - 将 WebView 向上滑动到隐藏 View 的顶部 - Swift

ios - 创建 viewcontroller 的实例并以编程方式对其进行 segue

ios - 如何改变 UITextField 的形状?

html - 不同平台上的 CSS 按钮

swift - 在 Swift Cocoa App 中实现 "Open file with"

iphone - 如何将导航栏(没有导航 Controller )和搜索栏添加到 TableView

ios - 将 UICollectionView header 中的 UISearchBar 隐藏在 NavigationBar 后面

ios - UISearchBar 和听写支持