Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISeachBar detect when user press Search button

I try to get notification when the Search button is clicked in the keyboard in the UISearchBar with this :

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

        return YES;
    }

But this method won't call when I press the Search button.

like image 820
YosiFZ Avatar asked Mar 28 '12 09:03

YosiFZ


4 Answers

Have you tried this delegate

Objective-C:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

Swift:

func searchBarSearchButtonClicked(searchBar: UISearchBar)

See the documentation for any searchbar delegates. https://developer.apple.com/documentation/uikit/uisearchbardelegate

like image 143
ipraba Avatar answered Nov 12 '22 22:11

ipraba


If you want to do something upon 'search' button get clicked then, Try this method

  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

Tells the delegate that the search button was tapped.

like image 45
hp iOS Coder Avatar answered Nov 12 '22 21:11

hp iOS Coder


You've to set the searchBar delegate first. In the viewDidLoad()

self.searchBar.delegate = self

then func searchBarSearchButtonClicked(searchBar: UISearchBar)

like image 4
jefferyleo Avatar answered Nov 12 '22 23:11

jefferyleo


Swift 5.X:

1) Set delegate:

searchBar.delegate = self

2) Add function:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
like image 1
Argus Avatar answered Nov 12 '22 23:11

Argus