I'm confused as to what the difference between a UISearchBar/UISearchBarDelegate and a UISearchBarController is.
Right now, this is what I'm doing to implement my search bar:
class SearchViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
}
extension SearchViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
if !searchBar.text!.isEmpty {
performSegue(withIdentifier: "Search", sender: searchBar.text!)
}
}
}
I've seen people online doing things like:
let searchController = UISearchController(searchResultsController: nil)
What is the difference between this and what I'm doing?
Your question is kind of similar to asking "What is the difference between UIView and UIViewController?"
UISearchBar is a view. It has a text field and (optionally) a button, and that's pretty much it.
UISearchController is a controller that controls the more abstract behaviour of "searching". One of its jobs is to control the behaviour of its searchBar, but it has many other jobs too, such as telling the searchResultsUpdater that it should update the search results (because the text in the search bar has changed, or whatever). It also exposes properties like obscuresBackgroundDuringPresentation and hidesNavigationBarDuringPresentation to control how the views should look when the search bar is the first responder.
If you are directly using UISearchBar, then you need to manually implement all these things that the UISearchController does for you. This could be desired if your intention is to create your own kind of searching behaviour that the UISearchController doesn't provide, but if you just want a plain old search feature in your app, use UISearchController because it will save you a lot of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With