NSSearchField occasionally causing an NSInternalInconsistencyException

1k Views Asked by At

The exception is "NSWindow: -_oldFirstResponderBeforeBecoming is not a valid message outside of a responder's implementation of -becomeFirstResponder." However, according to the stack trace the message is called from becomeFirstResponder, so I don't know what I am doing wrong. This is an intermittent error.

The user presses a key command sequence, which fires a menu item, which sends a message to the key window controller, which tells a search field on the window to become first responder.

Here's a summarised stack trace::

[NSException raise:format:]
[NSWindow _oldFirstResponderBeforeBecoming]
[NSSearchField becomeFirstResponder]
[MyWindowController focusSearchField]
[NSApplication sendAction:to:from:]
[NSMenu performKeyEquivalent:]

The implementation of the window controller's action is very simple:

public var searchField: NSSearchField?
@IBAction public func focusSearchField(sender: AnyObject) {
    searchField?.becomeFirstResponder()
}

Thanks for any assistance.

2

There are 2 best solutions below

0
On BEST ANSWER

I found a solution here.

Instead of using becomeFirstResponder, you should use NSWindow's makeFirstResponder method.

public var searchField: NSSearchField?
@IBAction public func focusSearchField(sender: AnyObject) {
    searchField?.window?.makeFirstResponder(searchField!)
}
0
On

I had this problem in a NSPopover with a textfield inside. When I was removing it i was not setting the contentViewController to nil and it was not deallocating. When I was trying to open another popover the exception happened, I understand that it is because the first responder was resigned from a view that was not in a window