NSSearchField continuously calls method

1.8k views Asked by At

I have created an NSSearchField that stores a list of recent searches. The search field is connected to a method that updates a label in the window with the text typed into the search field. The problem is that the label updates as soon as the user types into the search field, I need the label to not update until after the user types the text then hits the Enter key. I can do this with the typical NSTextField but I want to use the NSSearchField so I can show recent searches. Please see my code below and offer some suggestions. Thanks.

Interface file:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (weak) IBOutlet NSSearchField *searchField;
@property (weak) IBOutlet NSTextField *textField;

- (IBAction)searchString:(id)sender;

@end

Implementation file:

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize searchField, textField;

- (void)awakeFromNib {

    if ( [searchField respondsToSelector:@selector(setRecentSearches:)] ) {
        NSMenu *searchMenu = [[NSMenu alloc] initWithTitle:@"Search Menu"];
        [searchMenu setAutoenablesItems:YES];

        NSMenuItem *recentsTitleItem = [[NSMenuItem alloc] initWithTitle:@"Recent Searches" action:nil keyEquivalent:@""];
        [recentsTitleItem setTag:NSSearchFieldRecentsTitleMenuItemTag];
        [searchMenu insertItem:recentsTitleItem atIndex:0];

        NSMenuItem *norecentsTitleItem = [[NSMenuItem alloc] initWithTitle:@"No recent searches" action:nil keyEquivalent:@""];
        [norecentsTitleItem setTag:NSSearchFieldNoRecentsMenuItemTag];
        [searchMenu insertItem:norecentsTitleItem atIndex:1];

        NSMenuItem *recentsItem = [[NSMenuItem alloc] initWithTitle:@"Recents" action:nil keyEquivalent:@""];
        [recentsItem setTag:NSSearchFieldRecentsMenuItemTag];   
        [searchMenu insertItem:recentsItem atIndex:2];

        NSMenuItem *separatorItem = (NSMenuItem*)[NSMenuItem separatorItem];
        [separatorItem setTag:NSSearchFieldRecentsTitleMenuItemTag];
        [searchMenu insertItem:separatorItem atIndex:3];

        NSMenuItem *clearItem = [[NSMenuItem alloc] initWithTitle:@"Clear" action:nil keyEquivalent:@""];
        [clearItem setTag:NSSearchFieldClearRecentsMenuItemTag];
        [searchMenu insertItem:clearItem atIndex:4];

        id searchCell = [searchField cell];
        [searchCell setMaximumRecents:20];
        [searchCell setSearchMenuTemplate:searchMenu];
    }
}

- (IBAction)searchString:(id)sender {
    [textField setStringValue:[searchField stringValue]];
}

@end
2

There are 2 answers

1
zac0351 On BEST ANSWER

If you don't want to do it in code, view your nib file, select the search field and choose "Sends whole search string"

Like So:

Sends Whole Search String

Hope this helps!

1
rdelmar On

You need to use [searchField.cell setSendsWholeSearchString:YES]