Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [96]

By Root 953 0
and make the appropriate bindings. Then, when the user makes any changes in the predicate editor, the updated predicate will automatically be passed along to the array controller.

Adding a Predicate to the App Delegate


We’ll start by adding a new property to the app delegate. Open QuoteMonger_AppDelegate.h, which you’ll see looks just like the default app delegate header that was generated for MythBase. Add a new instance variable called searchPredicate and a matching property declaration. The interface declaration should now look something like this (new lines are in bold):

Now switch over to QuoteMonger_AppDelegate.m and add the following code near the top, just after the @implementation QuoteMonger_AppDelegate line.

The first line synthesizes a getter and setter for the searchPredicate property, and the init method creates a default value for it.

Now let’s hook up our controller to use this new predicate. Go back to Interface Builder, select the FoundQuotes controller, and bring up the Attributes Inspector. Select all the text in the Fetch Predicate text view and delete it, then switch to the Bindings Inspector. In the Controller Content Parameters section, open the Filter Predicate binding info. Select QuoteMonger_AppDelegate in the popup, type “searchPredicate” into the Model Key Path combo box, and press Return to turn on the binding.

Save your work, go back to Xcode, and click Build & Run. You should now see a different set of results, quotes spoken by a character named “Kramer” or containing the word “missed.” If nothing is matching, add a quote with one of these characteristics in order to test out the search window.

Add a Predicate Editor to the Search Window


We now have our FoundQuotes controller fetching values based on the contents of a predicate which is “owned” by the app delegate. The next step is to add an NSPredicateEditor to the search window, and configure it to let the user edit the predicate.

In Interface Builder, bring up the Quote Finder window. Make the whole window a bit taller, and drag the existing table view and text view down to the bottom. Then find an NSPredicateEditor in the Library, drag it into the empty space at the top of the window, then resize it to make it fill the available space. Figure 9-7 shows the idea.

Figure 9-7. The predicate editor is in place in our search window.

Configuring the Predicate Editor


Now it’s time to bind the editor to the app delegate’s predicate instance. Select the NSPredicateEditor (don’t forget the extra click to select the editor itself, not the scroll view that contains it), open the Bindings Inspector, and then examine the Value binding info. Select QuoteMonger_AppDelegate in the popup, then type in searchPredicate in the Model Key Path combo box, and press Return to activate this binding.

At this point, there’s only one more thing we need to do in order to enable searching with this predicate editor: we have to tailor it to the attributes we want to search on. NSPredicateEditor is a quite complex control, and fortunately most of its interesting features can be configured directly in Interface Builder. The predicate editor displays one or more NSPredicateEditorRowTemplate objects, which can each be configured to do searching in a variety of ways. You can make row templates that allow you to specify numbers or dates to compare against object values, or to pick from a list of predefined strings. In our case, we’re going to configure a row template that lets the user type a string in a text field to search by character names, show names, and quote contents. This row template can be reused, allowing the user to specify multiple search criteria simultaneously. In addition, another row template will let the user choose whether the search criteria must all be met (boolean AND) or whether it’s enough that any match succeeds (boolean OR) for a quote to appear in the results.

In Interface Builder, drill down into the predicate editor by clicking on the lower of the two rows you can see (the one containing popup buttons

Return Main Page Previous Page Next Page

®Online Book Reader