当用户清除他正在搜索的词时,我必须更改 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/