即使设置了可编辑行为,也无法在 NSPopover 上编辑 NSTextField [英] Not being able to edit NSTextField on NSPopover even though Editable behavior is set

查看:31
本文介绍了即使设置了可编辑行为,也无法在 NSPopover 上编辑 NSTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用 NSTextField 打开弹出窗口.文本字段不可编辑.文本字段的行为设置为 Editable.我仍然可以将文本粘贴和复制到此字段,但我无法对其进行编辑.

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(及其所有超级视图)接收键盘事件,但它不执行任何操作.发生这种情况是因为附加弹出窗口的视图位于不能成为关键窗口的窗口中.你不能以任何方式访问那个窗口,至少我不能.因此,解决方案是使用类别覆盖我们应用程序中每个 NSWindow 的 canBecomeKeyWindow 方法.

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

这使得弹出框完全响应.如果你需要另一个必须对 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆