Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchbar keyboard search button Action

I'm using UISearchBar when I input text on UISearchBar the keyboard shows. At that time, keyboard return key is "Search". I want to implement event when I press the keyboard search button. How can I implement the action? On UITextField it has

-(BOOL)textFieldShouldReturn:(UITextField *)textField;

But on UISearchBar it doesn't have return action.

Thank you for your helping.

like image 380
Chenggong Jin Avatar asked Jul 13 '13 08:07

Chenggong Jin


3 Answers

Add UISearchBarDelegate in .h

Also set SearchBar's object delegate to self.

Add this to the UISearchBarDelegate's method:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    // Do the search...
}

Swift

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}
like image 140
Milo Avatar answered Nov 07 '22 23:11

Milo


In Swift 3:

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
 }
like image 24
Kaptain Avatar answered Nov 07 '22 23:11

Kaptain


The textFieldShouldReturn method is a textField delegate method and not the one you're looking for. What you need is a UISearchBarDelegate method called searchButtonClicked, have a look here.

like image 6
Adis Avatar answered Nov 08 '22 00:11

Adis