Rock Snow
Rock Snow

Reputation: 9

How to "reset" the UISearchBar when back from another view showing the search results?

My 1st view has only UISearchBar, it will go to 2nd view to show the search results. When I click "back" button on the 2nd view, it come back to the 1st view with UISearchBar. I can use either UINavigation Controller or Segue to back to 1st view.

My question is how do I "reset" or "control" the SearchBar when it comes back to the 1st view? E.g., how do you "remove" the search-keywords which still left at the SearchBar, and also display the cursor inside the SearchBar?

enter image description here

Upvotes: 0

Views: 2118

Answers (2)

Daniel
Daniel

Reputation: 541

When you return to the First ViewController you can do yourSearchBar.text = "" to clear UISearchBar's text and yourSearchBar.becomeFirstResponder() to get keyboard up again. You can put this code to First ViewController's viewWillApear or to your unwind segue.

For detailed answer please add a code snippet...

Upvotes: 1

Bala
Bala

Reputation: 1304

When your UIViewController appear at the time viewWillAppear method will execute. Then put the reset code there.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    youSearchBar.text = ""
    youSearchBar.becomeFirstResponder()
}

Upvotes: 0

Related Questions