即使设置了可编辑行为,也无法在NSPopover上编辑NSTextField [英] Not being able to edit NSTextField on NSPopover even though Editable behavior is set
问题描述
我有一个应用程序,用 NSTextField
打开popover。文本字段不可编辑。文本字段的行为设置为可编辑
。我仍然可以粘贴并复制文本到这个字段,但我不能编辑它。
I have an application, which open popover with NSTextField
. The text field is not editable. Behavior for text field is set to Editable
. I still can paste and copy text to this field but i can't edit it.
任何人都知道,可能是错误?
Anyone knows, what can be wrong?
推荐答案
不知道你是否仍然需要答案,但可能还有一些人仍在看。我在苹果开发者论坛上找到了一个解决方案。引用原作者:
主要问题是键盘事件的工作原理。虽然NSTextField(及其所有的superviews)接收键盘事件,它不做任何动作。发生这种情况是因为弹出窗口所在的视图位于不能成为关键窗口的窗口中。你不能以任何方式访问该窗口,至少我不能。所以解决方案是覆盖方法canBecomeKeyWindow为我们的应用程序中的每个NSWindow使用类别。
The main problem is the way keyboard events works. Although the NSTextField (and all its superviews) receives keyboard events, it doesn't make any action. That happens because the view where the popover is atached, is in a window which can't become a key window. You can't access that window in any way, at least I couldn't. So the solution is override the method canBecomeKeyWindow for every NSWindow in our application using a category.
NSWindow+canBecomeKeyWindow.h
@interface NSWindow (canBecomeKeyWindow)
@end
NSWindow+canBecomeKeyWindow.m
@implementation NSWindow (canBecomeKeyWindow)
//This is to fix a bug with 10.7 where an NSPopover with a text field cannot be edited if its parent window won't become key
//The pragma statements disable the corresponding warning for overriding an already-implemented method
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
- (BOOL)canBecomeKeyWindow
{
return YES;
}
#pragma clang diagnostic pop
@end
这使得popover完全反应。如果你需要另一个窗口,必须响应canBecomeKeyWindow的NO,你总是可以做一个子类。
That makes the popover fully resposive. If you need another window which must respond NO to canBecomeKeyWindow, you can always make a subclass.
这篇关于即使设置了可编辑行为,也无法在NSPopover上编辑NSTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!