Question

guys! I have a NSSearchField in a window's toolbar. I tell the window to set its first responder to the search field: it works. Now: if I ask the window to tell me what's its first responder, it is NOT the search field even if the search field has the focus... Is there a way to set the search field as the window's first responder without having this mismatch?

Thank you

Was it helpful?

Solution

No. Or at least not easily. When a search field, or any simple text field, has keyboard focus, it isn't actually the first responder. When an instance of NSTextField (NSSearchField's superclass) is asked to become first responder, it asks the window for a "field editor", which is another text field used exclusively for editing. This editor is placed within the text field and set as the actual first responder.

To get the text field or search field, you could go up the view hierarchy using superview on the first responder. (I think it is the field editor's direct superview.) I'm pretty sure the actual text field is also set as the field editor's delegate, so you could also get it that way, but I don't remember where I read that.

More information about the field editor is available through the NSWindow class reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top