Learn Objective-C on the Mac - Mark Dalrymple [94]
We don’t want to include the NSBox in our layout, so select the box, and then select Layout➤Unembed Objects from the menu. Set the window’s title to Quote Finder, and rename the new array controller in your main nib window, probably called Quote Array Controller, to FoundQuotes.
A few adjustments need to be made to the table view. First of all, we don’t want any of the retrieved values to be editable here. This window is for search and display only. So bring up the Attributes Inspector, then select each of the table columns and click to disable the Editable button in the Inspector. We also want to display the show that each found quote belongs to, so add a column to the table view by duplicating one of the existing ones. Set the new column’s title (in the Attributes Inspector) to Show, and configure its Value binding (in the Bindings Inspector) to use the FoundQuotes controller, with arrangedObjects as the Controller Key and show.name as the Model Key Path. Make the table view a bit wider, and resize one or more of the columns so that they all fit.
Now make the window a bit taller, drag out an NSTextView from the Library, line it up beneath the table view, and resize it to match (see Figure 9-5). This text view will show the full quote of the selected item. Click to select the NSTextView (not the NSScrollView that contains it), turn off the Editable and Rich Text checkboxes in the Attributes Inspector, then switch back to the Bindings Inspector and configure the text view’s Value binding with FoundQuotes, selection, and quoteText.
Figure 9-5. First pass at the Query Finder window
We also need to make one change to the FoundQuotes controller, so that it will correctly reload and refilter its contents whenever the user makes changes in the Quotes controller. Select FoundQuotes, and bring up the Inspector Panel. Click to turn on the Auto Rearrange Content checkbox to enable this option.
Now switch back to Xcode, click Build & Run, and you’ll see that you now have two windows. The new Query Finder window simply shows all quotes you’ve entered with the data entry window. The rest of this chapter will show you how to change that using an NSPredicate, so that only the quotes a user searches for show up in this window.
Limiting Results with NSPredicate
As we’ve already mentioned, you can limit which records an NSArrayController prepares for display by using an NSPredicate. You can assign a predicate to an array controller directly in Interface Builder; from within your application code during initialization or whenever conditions change, warranting a refetch; or through Cocoa Bindings, which means that changes to a predicate can be automatically propagated to a controller. We’ll explore all of these options in this chapter.
Creating Predicates
The simplest way to create an NSPredicate is by using a format string containing attribute names, comparators, and values to compare against. The definition of a predicate looks a lot like a WHERE clause in SQL, and serves much the same purpose. Predicates are not limited to just Core Data usage, and can be applied to other areas of Mac OS X such as Spotlight. In its most basic form, you can define an NSPredicate like this:
NOTE: In C, if you have multiple inline string constants in your code separated only by whitespace (including carriage returns) they will all be concatenated together into a single character array, which can help format long strings in your code. This trick works equally well for inline NSString constants, just put a single