macos 强制 Cocoa 文本字段结束编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2284149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forcing a Cocoa text field to end editing
提问by Mike2012
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?
我有一个基于视图的 gui,其中包含每个 gui 项目的文本字段。当我选择另一个菜单项时,我希望我的文本字段退出编辑模式。无论如何,我在 nsTextField 文档中找不到这样做。有任何想法吗?
回答by Rob Keniger
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.
这将结束编辑并从文本字段中移除焦点。
回答by Stanislav Pankevich
The following solution from cocoabuilder.com - Stop edit session with a NSTextFieldworks for me both on OS X 10.9 and OS X 10.10 for Objective-C and on 10.10 for Swift.
来自cocoabuilder.com的以下解决方案- 使用 NSTextField 停止编辑会话在 OS X 10.9 和 OS X 10.10 for Objective-C 和 10.10 for Swift 上都适用于我。
Here is Objective-C version:
这是Objective-C版本:
@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).
背景:本主题中接受的解决方案在 OS X 10.9 和 10.10 上从来没有对我有用,所以不是[[textField window] makeFirstResponder:nil];
像它建议的那样调用- 我需要makeFirstResponder
用一些非 nil NSResponder 子类调用(这可以是 NSWindow 的内容视图或 NSViewController 的视图,两者都可以工作在我的情况下,或者一些假的 NSTextField 视图,对于更复杂的情况,用户看不到)。
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:)
重要细节:-[NSWindow makeFirstResponder]
必须在 NSTextField 的操作选择器中准确调用,就像我的示例演示的那样。NSTextField 方法管道中没有其他地方对我有用(textDidEndEditing:、textShouldEndEditing: 或 control:textShouldEndEditing:)
回答by joe
i know this is an old question, but in case anyone is wondering about swift 3, it only worked for me with
我知道这是一个老问题,但如果有人想知道 swift 3,它只对我有用
DispatchQueue.main.async {
self.window?.makeFirstResponder()
}
Swift 4.2 udpate, nil
makes the window its first responder. Otherwise throws error.
Swift 4.2 更新, nil
使窗口成为其第一响应者。否则抛出错误。
DispatchQueue.main.async {
self.window?.makeFirstResponder(nil)
}
回答by iSavaDevRU
This command should be used in viewWillLayout and if need in viewWillAppear and . And that's all. No subclasses and parallel threads.
这个命令应该在 viewWillLayout 中使用,如果需要的话在 viewWillAppear 和 . 就这样。没有子类和并行线程。
[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:
对于模态窗口,使用 viewDidAppear 或 dispatch_async:
dispatch_async(dispatch_get_main_queue(), ^{
[textField.window makeFirstResponder:nil];
});