Implementing the Target

A search field is a control object and so uses the target-action pattern: when a user does something to activate the search field, it sends an action message to a target object telling it to perform the search. Other than invoking the action method, NSSearchFieldCell and NSSearchField provide no support for textual searches. You must implement the search behavior yourself.

Simple Search

Typically you implement the action method in a controller object in your application. You need to find out what the search string is and then perform the search using the search term, as illustrated in Listing 1.

Listing 1  Simple action method for a search field’s target

- (IBAction)updateFilter:(id)sender {
 
    NSString *searchString = [searchField stringValue];
    /*
     Method continues to perform the search and display the results.
     */
}

Search Using a Search Category

If you want to support search categories (see Specifying a Search Category), you have to implement the action method invoked by the category menu items as well as that invoke by the search field itself. Your category action method should record in some way the menu item that was selected (typically by remembering the item’s tag). You might also set the placeholder string for the search menu (typically to the menu item’s title, to remind the user which item they chose), as illustrated in Listing 2.

Listing 2  Example search category menu item action method

- (IBAction)setSearchCategoryFrom:(NSMenuItem *)menuItem {
 
    self.searchCategory = [menuItem tag];
    [[self.searchField cell] setPlaceholderString:[menuItem title]];
}

In the search field’s action method, you need to find out what the search string is and then perform the search using the search term, taking into account any category that might have been set. A common way to represent the search is as an instance of NSPredicate. In Listing 3, searchCategory is set in the setSearchCategoryFrom: method illustrated in Listing 2.

Listing 3  Example action method for a search field’s target

- (IBAction)updateFilter:(id)sender {
    /*
     Create a predicate based on what is the current string in the
     search field and the value of searchCategory.
     */
    NSString *searchString = [self.searchField stringValue];
    NSPredicate *predicate;
 
    if ((searchString != nil) && (![searchString isEqualToString:@""])) {
        if (searchCategory == 1) {
            predicate = [NSPredicate predicateWithFormat:
                                     @"firstName contains[cd] %@", searchString];
        }
        if (searchCategory == 2) {
            predicate = [NSPredicate predicateWithFormat:
                                     @"lastName contains[cd] %@", searchString];
        }
    }
 
    /*
     Method continues to perform the search and display the results.
     */
}

The Cancel Button

Clicking the cancel button behaves exactly as if the user selected all the text in the field and deleted it. The action message is sent to the target as usual. The target would typically see that the string in the field is the empty string, and so wouldn't filter anything.