Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar detect when user stop type in swift

Most of the example I found is in Objective - C which is very hard for me to understand can someone provide an example regarding this question in Swift. Here is one solution but it is in Objective C.

like image 729
user2273146 Avatar asked Dec 09 '16 11:12

user2273146


1 Answers

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()
}
like image 59
user2273146 Avatar answered Oct 14 '22 05:10

user2273146