Database Reference
In-Depth Information
thanks to KVO, any tables or interface elements associated with that
NSArray-
Controller
will get updated automatically and instantly. This means if we put a
search field into our application and link it to our
NSArrayController
of
Recipe
objects, our source list of recipes will automatically be filtered based on the
user input into that
NSSearchField
. Even better, we don't have to write any code!
All we need to do to implement it is configure the bindings for the
NSSearchField
.
To accomplish this, we first add an
NSSearchField
to our application. In
Figure
32,
Adding an NSSearchField to our application
, on page 150
, we have decreased
the vertical size of the recipe source list and inserted an
NSSearchField
above it.
Next, we configure its bindings.
Figure 32—Adding an
NSSearchField
to our application
As shown in
Figure 33,
NSSearchField bindings
, on page 151
, the
NSSearchField
interface element works with an
NSPredicate
. We write the predicate in the
Predicate Format field, substituting
$value
for whatever the user inputs into
the search field and using the controller key and value transformer to bind
it to our data. In this example, we want to filter on the name of recipes;
therefore, we bind the
NSSearchField
to our recipe's
NSArrayController
using the
controller key of
filterPredicate
and a predicate of
namecontains[c]$value
.
Once we add one predicate, another appears on the Bindings tab for the
NSSearchField
. This is so we can use a search field for more than one type of
search. Each search will be shown in the drop-down on the
NSSearchField
, and
the Display Name binding will be shown to the user. This allows us to create
one
NSSearchField
that can search for recipe names, ingredients, descriptions,
or anything else we may need.
Once the binding is complete, we are done adding a basic search field. Running
the application shows that text entered into the search field impacts the list