How to stop focus leaving an NSSearchField when creating a popup as user types

Discussion in 'Mac Programming' started by Anim, Feb 26, 2014.

  1. Anim macrumors 6502a

    Anim

    Joined:
    Dec 16, 2011
    Location:
    Macclesfield, UK
    #1
    I have a Search field in my OSX app that shows results in a NSPopover, much like how Spotlight works.

    The problem is after the first letter is typed and I show the search results in a popup then my search field loses focus so the user only gets to enter one character.

    Launch Popover on first letter in search box....
    Code:
    [FONT="Courier New"] 
    [[self searchResultsPopover]
              showRelativeToRect:[[notification object] bounds]
              ofView:[notification object] preferredEdge:NSMaxYEdge];
                
    [self.searchField becomeFirstResponder];[/FONT]
    
    I can set firstResponder back to the search field (as shown above) after the popup appears but then it highlights the text. Ideally I want to show the popup but not select it at all until the user clicks within it. Just like how spotlight works.

    Everything denies first responder in the popup via IB but this has little effect.

    Any ideas?

    Thanks
    Anim
     
  2. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #2
    Subclass the search field and override
    Code:
    - (BOOL)resignFirstResponder
    to return NO when necessary.
     
  3. Anim thread starter macrumors 6502a

    Anim

    Joined:
    Dec 16, 2011
    Location:
    Macclesfield, UK
    #3
    Thats the one, thank you. I was trying to fix it from the other end haha.

    Cheers
     
  4. Anim thread starter macrumors 6502a

    Anim

    Joined:
    Dec 16, 2011
    Location:
    Macclesfield, UK
    #4
    Actually, that didn't work when subclassing NSSearchfield, If I override the resignsFirstResponder then I can't type into the actual search field cell. I can't subclass the NSSearchFieldCell because it doesn't respond to resignsFirstResponder.

    Hmm.

    I read that I can sub class NSView to detect a mouse click outside the search field and then set [searchField.window makeFirstResponder:nil] which allows the user to break out of it but still can't lock them into it in the first place.

    My NSSearchField i set to update continuously too.

    Any other ideas? It is the NSPopover that is making the search field lose focus. I will just try making it visible / invisible as the user types instead and see how that works.
     
  5. Anim thread starter macrumors 6502a

    Anim

    Joined:
    Dec 16, 2011
    Location:
    Macclesfield, UK
    #5
    Solved it by just ordering the NSPanel state rather than working on the NSPopover

    Code:
    // hide it
                 [[self searchResultsPanel] orderOut:nil];
                
    // show it without losing focus to current control
                [[self searchResultsPanel] orderBack:nil];
     

Share This Page