How can I programmatically force a search in the UISearchBar?

后端 未结 8 1228
遥遥无期
遥遥无期 2021-02-05 05:15

How can I force the UISearchBar to automatically start a new search (like pressing the Search button)? Is there an easy way to achieve this?

相关标签:
8条回答
  • 2021-02-05 05:58

    You can force your searchBar,

    Objective C:

    [self searchBar:yourSearchBarName textDidChange: yourSearchingString];
    

    Swift 3.0:

    self.searchBar(self.yourSearchBarName, textDidChange: yourSearchingString)
    

    This will trigger your searchBar.

    0 讨论(0)
  • 2021-02-05 06:00

    Just have the search input field become the first responder:

    [self.searchInputField becomeFirstResponder];
    
    0 讨论(0)
  • 2021-02-05 06:01

    This worked - Swift 4

       searchBar.text="Amber"
       self.searchBar(self.searchBar, textDidChange: "Amber")
    
    0 讨论(0)
  • 2021-02-05 06:08

    After making sure that the text you want is inside your search bar you can call self.searchBarSearchButtonClicked(searchBar) (passing in your search bar) assuming you have already implemented this UISearchBarDelegate method searchBarSearchButtonClicked(_ searchBar: UISearchBar)

    0 讨论(0)
  • 2021-02-05 06:08
    let searchBar: UISearchBar = /* The Searchbar */
    searchBar.delegate?.searchBar?(searchBar, textDidChange: "")
    searchBar.text = ""
    
    0 讨论(0)
  • 2021-02-05 06:11

    Yes you only need to make an implicit call to the searchBarSearchButtonClicked: on your UISearchbarDelegate. Have a look to the official doc for more info about this delegate.

    0 讨论(0)
提交回复
热议问题