How to remove focus from an NSTextField

Sometimes you need to make your text field give up first responder status, and therefore end editing. You’d think that – looking at the NSControl Class Reference – we should call something along the lines of validateEditing or abortEditing. But that doesn’t work.

Instead, we can set our text field’s window to “no first responder”. This approach works with any NSControl element:

[self.textfield.window makeFirstResponder:nil];

This approach is similar to telling a UITextField to resignFirstResponder (the old “invisible button to dismiss the iOS keyboard” ploy).

Leave a Reply