Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/UIKit.framework |
Availability | Available in iOS 3.0 and later. |
Declared in | UISearchDisplayController.h |
A search display controller manages display of a search bar and a table view that displays the results of a search of data managed by another view controller.
You initialize a search display controller with a search bar and a view controller responsible for managing the original content to be searched. When the user starts a search, the search display controller is responsible for superimposing the search interface over the original view controller’s view and showing the search results. The results are displayed in a table view that’s created by the search display controller. In addition to the original view controller, there are logically four other roles. These are typically all be played by the same object, often the original view controller itself.
The search results table view’s data source.
This object is responsible for providing the data for the results table.
The search results table view’s delegate.
This object is responsible for, amongst other things, responding to the user’s selection of an item in the results table.
The search display controller’s delegate.
The delegate conforms to the UISearchDisplayDelegate
protocol. It is notified of events such as when the search starts or ends, and when the search interface is displayed or hidden. As a convenience, it may also be told about changes to the search string or search scope, so that the results table view can be reloaded.
The search bar’s delegate.
This object is responsible for responding to changes in the search criteria.
Typically you initialize a search display controller from a view controller (usually an instance of UITableViewController
) that’s displaying a list; you set self
for the search display controller’s view controller and search results data source and delegate:
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 ...; |
} |
Important: Any given view controller or search bar can only be associated with a single search display controller at a time. If a search display controller is destroyed (for example, in response to a memory warning), then you can create a new one and associate it with the original view controller or search bar.
active
property
– setActive:animated:
delegate
property
searchBar
property
searchContentsController
property
searchResultsTableView
property
searchResultsDataSource
property
searchResultsDelegate
property
For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.
The visibility state of the search interface.
@property(nonatomic, getter=isActive) BOOL active
The default value is NO
.
If you set this value directly, any change is performed without animation. Use setActive:animated:
if a change in state should be animated.
When the user focus in the search field of a managed search bar, the search display controller automatically displays the search interface. You can use this property to force the search interface to appear.
UISearchDisplayController.h
The controller’s delegate.
@property(nonatomic, assign) id<UISearchDisplayDelegate> delegate
UISearchDisplayController.h
The search bar. (read-only)
@property(nonatomic, readonly) UISearchBar *searchBar
UISearchDisplayController.h
The view controller that manages the contents being searched. (read-only)
@property(nonatomic, readonly) UIViewController *searchContentsController
This is typically an instance of UITableViewController
.
UISearchDisplayController.h
The data source for the table view in which the search results are displayed.
@property(nonatomic, assign) id<UITableViewDataSource> searchResultsDataSource
The default is nil
.
UISearchDisplayController.h
The delegate for the table view in which the search results are displayed.
@property(nonatomic, assign) id<UITableViewDelegate> searchResultsDelegate
The default is nil
.
UISearchDisplayController.h
The table view in which the search results are displayed. (read-only)
@property(nonatomic, readonly) UITableView *searchResultsTableView
This method creates a new table view if one does not already exist.
UISearchDisplayController.h
Returns a display controller initialized with the given search bar and contents controller.
- (id)initWithSearchBar:(UISearchBar *)searchBar contentsController:(UIViewController *)viewController
A search bar.
The search bar must not currently be associated with another search display controller.
The view controller that manages display of the original contents that are to be searched.
The view controller must not currently be associated with another search display controller.
A display controller initialized with the given search bar and contents controller.
UISearchDisplayController.h
Displays or hides the search interface, optionally with animation.
- (void)setActive:(BOOL)visible animated:(BOOL)animated
YES
to display the search interface if it is not already displayed; NO
to hide the search interface if it is currently displayed.
YES
to use animation for a change in visible state, otherwise NO
.
When the user focus in the search field of a managed search bar, the search display controller automatically displays the search interface. You can use this method to force the search interface to appear.
UISearchDisplayController.h
Last updated: 2009-08-13