NSSearchField Continuously Calls a Method

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:

 #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 
+4
source share
2 answers

If you don’t want to do this in code, browse your nib file, select the search field and select "Submit the entire search bar"

Like so:

Sends Whole Search String

Hope this helps!

+6
source

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

+1
source

Source: https://habr.com/ru/post/1412892/


All Articles