Introduction to Search Fields
A search field is a rounded text field that displays text that the user can select or edit, and that sends its action message to its target when the user presses the Return key. It presents a standard user interface for searches, including a search button, a cancel button, and a pop-up icon menu for listing recent search strings and custom search categories. The search button includes a menu and the option to send the results while the user is typing or when the user presses the Return key. If there is no text in the search field, the cancel button is hidden. Figure 1 shows the major components of a search field.
At a Glance
A search field is implemented by two classes:
NSSearchFieldCell, the cell that does most of the work, and
NSSearchField, the control that contains that cell.
There are, broadly speaking, two ways to configure and use a search field—programmatically, or with Cocoa bindings.
If you configure a search field programmatically, you should set the target and action of the control or its cell to the receiver that is interested in the search request. Also, remember that
NSSearchFieldclasses are direct subclasses of
NSTextField, respectively, so you can use all the methods inherited from these classes.
If you use bindings, you typically set the multi-value
Predicatebinding to set the predicates on a controller such as an instance of
You can, of course, mix these approaches—for example, you may need to programmatically update the
Predicate binding if you dynamically change the search categories based on the visibility of columns in a table view.
How to Use This Document
To learn how to add a search field to your application, using either a xib file or in code, read “Adding a Search Field to Your Application.”
To learn how to set up the search field’s pop-up icon menu to show recent search strings and search categories, read “Adding a Search Field to Your Application.”
To learn how to implement suitable methods in the search field’s target, read “Adding a Search Field to Your Application.”
To learn how to change the appearance of a search field programatically, read “Customizing Your Search Field’s Appearance.”