UISearchBar检测用户何时在swift中停止输入

时间:2016-12-09 11:29:42

标签: ios swift uisearchbar

我发现的大多数示例都在Objective - C,我很难理解,有人可以在Swift中提供有关此问题的示例。 Here是一种解决方案,但它位于Objective C

5 个答案:

答案 0 :(得分:5)

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    print("after every text gets changed")
    timer.invalidate()
    timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: #selector(ViewController.output), userInfo: searchText, repeats: false)
}

func output(){
    print("hello")
    if timer.userInfo != nil {
        print(timer.userInfo)
    }
    timer.invalidate()
}

答案 1 :(得分:1)

您必须使用UISearchBarDelegate检查它:

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    //DO what you want
}

请记住添加:

searchBar.delegate = self

他的相关协议:

UISearchBarDelegate

https://developer.apple.com/reference/uikit/uisearchbardelegate

当键盘消失时调用该委托:searchBarTextDidEndEditing,所以将该代码添加到您的项目中:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

答案 2 :(得分:0)

链接的Swift版本:

var searchDelayer: Timer!

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchDelayer.invalidate(), searchDelayer = nil
    if true {
        searchDelayer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.doDelayedSearch), userInfo: searchText, repeats: false)
    }
}

func doDelayedSearch(_ t: Timer) {
    assert(t == searchDelayer)
    self.request(searchDelayer.userInfo!)
    searchDelayer = nil
}




var priorSearchText = ""

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.request), object: priorSearchText)
        priorSearchText = searchText
    }
    if true {
        self.performSelector(#selector(self.request), withObject: searchText, afterDelay: 1.5)
    }

答案 3 :(得分:0)

您可以尝试以下方法:

class WorkItem {

private var pendingRequestWorkItem: DispatchWorkItem?

func perform(after: TimeInterval, _ block: @escaping VoidBlock) {
    // Cancel the currently pending item
    pendingRequestWorkItem?.cancel()

    // Wrap our request in a work item
    let requestWorkItem = DispatchWorkItem(block: block)

    pendingRequestWorkItem = requestWorkItem

    DispatchQueue.main.asyncAfter(deadline: .now() + after, execute: requestWorkItem)
}
}

//使用

lazy var workItem = WorkItem()

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

    // 0.5 == half second
    workItem.perform(after: 0.5) {

       //Have Stopped, do something.
    }
}

参考

Link 1 by swiftbysundell

Link 2 on github

答案 4 :(得分:0)

雨燕5

节流搜索

使用NSObject.cancelPreviousPerformRequests取消以前的触发器。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(ViewController.reload), object: nil)
    self.perform(#selector(ViewController.reload), with: nil, afterDelay: 0.5)
}

@objc func reload() {
    guard let searchText = searchBar.text else { return }
    search(searchText)
}