Bob
Bob

Reputation: 551

Benefits of using UISearchController?

What's the benefit of using UISearchController over UISearchBarDelegate? The difference seems kind of subtle to me. In particular, updateSearchResults seems essentially the same as textDidChange. Assuming we don't use a new VC to display the search results, I can't see why one should add UISearchController on top of UISearchBarDelegate.

Upvotes: 2

Views: 1019

Answers (1)

rmaddy
rmaddy

Reputation: 318884

From the documentation for UISearchController:

You use a search controller in tandem with your existing view controllers. When you have a view controller with searchable content, incorporate the search bar of a UISearchController object into your view controller’s interface. When the user interacts with that search bar, the search controller automatically displays a new view controller with the search results that you specify.

Each search controller provides a UISearchBar object that you must incorporate into the user interface of your initial view controller. Add this object to the view containing your searchable contents. When the user taps the search bar to enter a search term, the search controller automatically displays your search results view controller and notifies your app that the search process has begun.

Basically, the search controller handles the grunt work of displaying the search results controller, animating the search bar to the header, graying out the results if there is nothing entered, etc.. It does a lot of the grunt work for you when switching between your regular view controller and the results controller.

When you don't need (or want) all of that helpful work done for you and you want to show your own search bar, or your main view controller can easily show the search results, then you don't need to use a UISearchController.

This is similar to asking why you should use a UINavigationController when you can add your own UINavigationBar and delegate or why use a UITabBarController when you can add your own UITabBar and delegate. It's all a matter of convenience and user interface consistency.

Upvotes: 7

Related Questions