I created an NSSearchField
that stores a list of recent queries. The search field is connected to a method that updates the label in the window with the text entered in the search field. The problem is that the shortcut is updated as soon as the user types in the search field, I need the label not to be updated until the user types the text and presses the Enter key. I can do this with a typical NSTextField
, but I want to use NSSearchField
so that I can show recent queries. See my code below and suggest some suggestions. Thanks.
Interface File:
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
source share