I have a view based gui that contains a text field for each gui item. When I select another menu item I want my text fields to exit out of editing mode. I could not find anyway to do this in the nsTextField docs. Any ideas?
4 Answers
You need to tell the window to remove first responder status from the control:
[[textField window] makeFirstResponder:nil];
This will end editing and remove focus from the text field.
-
1Works fine in Yosemite here. Are you calling from the main thread?
dispatch_async(dispatch_get_main_queue(), ^{[textField.window makeFirstResponder:nil];});
Nov 3, 2014 at 7:32 -
I'm hitting a similar issue when I have a focused
NSTextField
and click on anNSSlider
... I can add amouseDown
handler toNSSlider
to grab focus, but I'm wondering if there is a good way to bake this intoNSTextField
itself, so it looses edit mode upon clicking anyNSControl
... Apr 24, 2015 at 4:01 -
Would this work if called directly on window inside window controller's
windowDidLoad
method in swift? I assume it would have the same effect, but not working for me. Or am I confusing this with something else? Nov 16, 2015 at 18:27 -
windowDidLoad
does not necessarily mean that the window is key. You could use theNSWindowDelegate
methodwindowDidBecomeKey(_ notification: NSNotification)
and when the window becomes the key window, make the first respondernil
. You would have to manage this so that you can actually make the window accept first responder status :-) Nov 17, 2015 at 0:51 -
[self.view.window makeFirstResponder:nil]
may also work in most cases, and is more generic.– ElySep 5, 2018 at 21:24
The following solution from cocoabuilder.com - Stop edit session with a NSTextField works for me both on OS X 10.9 and OS X 10.10 for Objective-C and on 10.10 for Swift.
Here is Objective-C version:
@implementation CustomTextField
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
self.target = self;
self.action = @selector(action:);
return self;
}
- (void)action:(NSTextField *)textField {
// Make something else first responder, something neutral
// (or can be a fake invisible element)
[self.window makeFirstResponder:self.window.contentView];
// Other code
}
@end
Background: accepted solution in this topic never worked for me on OS X 10.9 and 10.10 so instead of calling [[textField window] makeFirstResponder:nil];
as it suggests - I needed to call makeFirstResponder
with some non-nil NSResponder subclass (this can be either NSWindow's content view or NSViewController's view that both work in my case or some fake NSTextField view which is not visible to user for more complex cases).
Important detail: -[NSWindow makeFirstResponder]
must be invoked exactly within NSTextField's action selector like my example demonstrates. No other place in NSTextField method pipeline did work for me (textDidEndEditing:, textShouldEndEditing: or control:textShouldEndEditing:)
-
1even though strange, this also was the only solution that worked for me. Sep 27, 2015 at 19:43
-
1indeed this works while the accepted answer doesn't, in Yosemite Nov 12, 2015 at 23:47
-
If you had called the method from the answer above in viewWillLayout, you would have done everything in one row without creating any subclasses. Feb 11, 2018 at 22:59
-
This solution works for me, but only when Enter is pressed, it still doesn't work when TAB button is pressed.– rkudinovSep 9, 2019 at 20:39
i know this is an old question, but in case anyone is wondering about swift 3, it only worked for me with
DispatchQueue.main.async {
self.window?.makeFirstResponder()
}
Swift 4.2 udpate, nil
makes the window its first responder. Otherwise throws error.
DispatchQueue.main.async {
self.window?.makeFirstResponder(nil)
}
-
I don't know about saving my life but this really helped. :) I also want to point out that in my case it was not makeFirstResponder but resignFirstResponder that had to be wrapped in the Dispatch Queue.– ChrisOct 19, 2017 at 17:30
This command should be used in viewWillLayout and if need in viewWillAppear and . And that's all. No subclasses and parallel threads.
[textField.window makeFirstResponder:nil];
This works well with the main window. But this does not work well with modal windows.
For modal windows use viewDidAppear or dispatch_async:
dispatch_async(dispatch_get_main_queue(), ^{
[textField.window makeFirstResponder:nil];
});