Search code examples
xcodecocoanstextfieldnsbuttonnssearchfield

NSTextField validation and NSSearchField save it's value to recent history on NSButton click


I have this setup, my NSWinow has two NSSearchFileds with relative autosave search history, one NSTextField and a NSButton.

NSSearchFiled has to autosave the search (wich work right when user press "Enter" key) NSTextField needs to be validated and saved to user prefs (by it's action).

The problem is that if i have focus on NSSearchFiled and click on NSButton the string is not autosaved to recents, if i have focus on NSTextField and click on NSButton the value is not validated and saved to user prefs...

I've tried this in NSButton action:

// SearchF_Name is of NSSearchFiled
[SearchF_Name sendAction:SearchF_Name.action to:SearchF_Name.target];

with no luck, and googling doesn't help either... Con you help me?


Solution

  • After asking the question I've tried the "last option"... and it worked!

    this is the code for - (IBAction)Btn_SearchAction:(id)sender

    NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
    if ([firstResponder isKindOfClass:[NSText class]] && [(NSText *)firstResponder delegate] == TextF_ResultLimit) {
        NSLog(@"SearchDelegate::Btn_SearchAction TextF_ResultLimit has focus, let's save it's value");
        [TextF_ResultLimit performClick:self];
    }
    
    NSLog(@"SearchDelegate::Btn_SearchAction SearchF_Name saving search value to recent history");
    [SearchF_Name performClick:self];