ios - 如何在 SearchBar 中使用 textDidChange 进行搜索

标签 ios swift xcode uisearchbar

我已经创建了一个搜索栏并正在使用这个功能:

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

    getAlamoWithSearchText(withOffsetValue: 0)
    print("Here it is: \(posts.count)")
}

我如何实现一个计时器,使其仅在用户停止输入 1 秒后才发生变化?

最佳答案

有多种方法可以实现这一点。其中之一是在类范围内添加一个计时器。然后在 textDidChange 委托(delegate)方法中使计时器无效。在此下方添加一条语句以将“repeat”参数设置为 false 的方法调用安排。

在被调用的方法中,调用您的 API 来搜索元素。

关于ios - 如何在 SearchBar 中使用 textDidChange 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44091979/

相关文章:

点击之间的 iOS 计时

ios - 从 View Controller Xcode 传回数据

可以抛出和不能抛出的方法中的 Swift 类型推断

ios - Xcode 6.1 对 iPhone 6 和 4s 模拟器使用相同的 2x 图像

ios - 不将 WKWebView 保留在安全区域内的约束

ios - 如何在 Xcode 中自动检查缺失的本地化?

ios - 带有协议(protocol)的自定义 Cell,用于检查 UISwitch 中的更改

ios - 设置Tableview的backgroundView

iphone - 应用程序中的多个 NSTimer

swift - 目录文件夹中的文件日期