Class

UISearchDisplayController

Important: UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.

Overview

A search display controller manages the display of a search bar, along with a table view that displays search results.

You initialize a search display controller with a search bar and a view controller responsible for managing the data to be searched. When the user starts a search, the search display controller superimposes the search interface over the original view controller’s view and shows the search results in its table view.

In addition to managing the searchable data, the original view controller typically plays four more roles you need to fill when using a search display controller. Those roles are the following:

  1. Data source for the search results table view (searchResultsDataSource), which provides the data for the results table.

  2. Delegate for the search results table view (searchResultsDelegate), which responds to the user’s selection of an item in the results table.

  3. Delegate for the search display controller (delegate), which responds to events such the starting or ending of a search, and the showing or hiding of the search interface.

    As a convenience, this delegate may also be told about changes to the search string or search scope, so that the results table view can be reloaded.

  4. Delegate for the search bar (delegate described in UISearchBar), which responds to changes in search criteria.

Typically, you initialize a search display controller from a view controller (usually an instance of UITableViewController) that’s displaying a list. See the Simple UISearchBar with State Restoration sample code project for an example of how to configure a search display controller in Interface Builder. To perform configuration programmatically, set self for the search display controller’s view controller and search results data source and delegate, as shown here:

searchController = [[UISearchDisplayController alloc]
                         initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

If you follow this pattern, then in the table view data source and delegate methods you can check the methods’ table view argument to determine which table view is sending the message:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    if (tableView == self.tableView) {
        return ...;
    }
    // If necessary (if self is the data source for other table views),
    // check whether tableView is searchController.searchResultsTableView.
    return ...;
}

Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.

Topics

Initializing a Search Bar

init(searchBar: UISearchBar, contentsController: UIViewController)

Returns a display controller initialized with the given search bar and contents controller.

Displaying the Search Interface

var isActive: Bool

The visibility state of the search interface.

func setActive(Bool, animated: Bool)

Displays or hides the search interface, optionally with animation.

Configuring a Search Bar

var delegate: UISearchDisplayDelegate?

The controller’s delegate.

var searchContentsController: UIViewController

The view controller that manages the contents being searched.

var searchResultsTableView: UITableView

The table view in which the search results are displayed.

var searchResultsDataSource: UITableViewDataSource?

The data source for the table view in which the search results are displayed.

var searchResultsDelegate: UITableViewDelegate?

The delegate for the table view in which the search results are displayed.

var searchResultsTitle: String?

The title for the search results view.

var displaysSearchBarInNavigationBar: Bool

Specifies that the navigation bar contains a search bar.

var navigationItem: UINavigationItem?

Represents the search display controller in a navigation controller’s navigation bar.

Relationships

Inherits From

Conforms To

See Also

Deprecated Classes

class UIActionSheet

Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of actionSheet.

Deprecated
class UIAlertView

In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).

Deprecated
class UIDocumentMenuViewController

A list of all the available document providers for a given file type and mode, in addition to custom menu items that you add.

Deprecated
class UILocalNotification

Important:UILocalNotification is deprecated in iOS 10. Use UNNotificationRequest instead. A UILocalNotification object specifies a notification that an app can schedule for presentation at a specific date and time.

Deprecated
class UIMutableUserNotificationAction

Important: UIMutableUserNotificationAction is deprecated in iOS 10. Use UNNotificationAction instead. A UIMutableUserNotificationAction object represents a modifiable version of the UIUserNotificationAction class.

Deprecated
class UIMutableUserNotificationCategory

Important: UIMutableUserNotificationCategory is deprecated in iOS 10. Use UNNotificationCategory instead. A UIMutableUserNotificationCategory object encapsulates information about custom actions that your app can perform in response to a local or push notification. Use instances of this class to customize the actions included in an alert when space onscreen is constrained.

Deprecated
class UIPopoverController

Important: UIPopoverController is deprecated in iOS 9. In iOS 9 and later, a popover is implemented as a UIViewController presentation. To create a popover, use an instance of UIPopoverPresentationController and specify the popover style.

Deprecated
class UIStoryboardPopoverSegue

The UIStoryboardPopoverSegue class defines a specific type of segue for presenting content in a popover. For popover segues, the destination view controller contains the content to be displayed in the popover. This class provides an additional popoverController property so that your custom code has access to the popover controller object. For example, you might want to store the popover controller elsewhere in your code so that you can dismiss the popover programmatically.

Deprecated
class UIUserNotificationAction

Important: UIUserNotificationAction is deprecated in iOS 10. Use UNNotificationAction instead. A UIUserNotificationAction object represents a custom action that your app can perform in response to a remote or local notification.

Deprecated
class UIUserNotificationCategory

Important:UIUserNotificationCategory is deprecated in iOS 10. Use UNNotificationCategory instead. A UIUserNotificationCategory object encapsulates information about custom actions that your app can perform in response to a local or push notification.

Deprecated
class UIUserNotificationSettings

Important: UIUserNotificationSettings is deprecated in iOS 10. Use UNNotificationSettings instead. A UIUserNotificationSettings object encapsulates the types of notifications that can be displayed to the user by your app.

Deprecated