1
votes

I am implementing a child borderless search results window that floats under a NSSearchField and displays the results of the search as the user types. The NSWindow does not need to be key because the NSSearchField forwards appropriate commands (up arrow, down arrow, enter key) to a custom search results view in the window which let's the user navigate through the results anytime during the search. This all works great except for one use case: I want to easily close the window when the user clicks anywhere else in the app.

I would normally do this in the windowDidResignKey notification but this won't be sent in this case since the window is never key. Is there another way to easily handle this situation?

--Edit with solution--

Using a variation of Rob's idea below I simply implemented controlTextDidEndEditing:, a delegate method of NSSearchField, which is called (among other times) when the search field loses focus. If the search field is not my main parent window's first responder I close the search results window.

1
That's a better solution than my answer because it doesn't require subclassing.Rob Keniger

1 Answers

1
votes

You could subclass NSWindow and override makeFirstResponder: so that any time the first responder changes, the results window is closed if it's open. The first responder shouldn't change while your search field is active, so the window should only be closed once the search field's field editor loses focus.