SWIFT iOS9:如何识别UISearchBar的X按钮上的单击

时间:2015-11-13 19:53:16

标签: iphone swift2 ios9 uisearchbar xcode7

当用户清除他正在搜索的单词时,我必须更改布尔变量值。当他点击搜索栏右侧的X图标时,他会清除它。

我不知道如何采取这一行动。与搜索栏相关的功能仅识别取消按钮,书签按钮和搜索结果按钮的点击次数。

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

2 个答案:

答案 0 :(得分:1)

您可以实现UISearchBarDelegate的这个委托功能:

optional func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)

正如文件所说:

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

答案 1 :(得分:0)

快速4 UISearchResultsUpdating委托方法:

func updateSearchResults(for searchController: UISearchController) 
{
    print("updating: \(searchController.searchBar.text!)")

    if (searchController.searchBar.text?.isEmpty)! 
    {
       print("empty")
    }
}