简体   繁体   中英

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.

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

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

self.searchBar.delegate = self

then func searchBarSearchButtonClicked(searchBar: UISearchBar)

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.

Swift 5.X:

1) Set delegate:

searchBar.delegate = self

2) Add function:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM