UISearchControlleris a new controller in iOS 8 to handle search.
Prior to iOS 8, we have UISearchDisplayController, which is deprecated. UISearchControllermore than replaced it, with a architectural change.
There are two View Controllers (VC) involved in search. Let’s call them:
Presenting VC- where the search is triggered
Results VC- where the results are displayed in
You may also have the presenting VC display the results. In that case, they are the same VC.
UISearchControlleralso provides a UISearchBarobject, because all search requires the search bar for input.
Triggering the search
There are 2 ways.
1. Add Search Bar
Add the UISearchBarto your presenting VC.
This must be done programatically, because there is no library object for UISearchControllerin Xcode. And you have to use it’s searchBarobject.
If you are using a UITableView, a search bar can be added to the header easily:
Remember: searchControlleris a UIViewController(read in later section) so it can be called with presentViewController.
When the presenting VC has a navigation bar, you will need to configure search controller:
UISearchResultsUpdating protocol has a callback when the user enters into the search bar.
Set searchResultsUpdater. Typically, the results VC will implement the protocol, so that it will update the results accordingly.
Set delegateto the view controller that handles the calls, typically the presenting VC.
searchController.delegate = self
UISearchController is a UIViewController
UISearchControllerinherits from UIViewController.
You can present it modally with presentViewController.
BUT, you should never push to navigation controller or use it as a child etc. If you want that, you can use UISearchContainerViewControllerto wrap it first.