带有NSPopover和NSTextField的NSStatusItem [英] NSStatusItem with NSPopover and NSTextField

查看:117
本文介绍了带有NSPopover和NSTextField的NSStatusItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSStatusItem,它显示一个NSPopover,其中包含一个NSTextField,但该文本字段不可编辑,尽管在Xcode中是这样.现在,这是一个已知的错误,显然有人在此处发布了一个解决方案.我真的需要解决此错误.

I have a NSStatusItem that displays a NSPopover which contains a NSTextField but the text field isn't editable although it has been so be so in Xcode. Now this is a known bug and there is apparently a solution someone posted here. I really need to work around this bug.

为方便起见,我在这里引用答案:

I'll just quote the answer here for convenience:

主要问题是键盘事件的工作方式.尽管NSTextField(及其所有超级视图)都接收键盘事件,但他没有执行任何操作.之所以发生这种情况,是因为弹出窗口所在的视图位于一个不能成为关键窗口的窗口中.您无法以任何方式访问该窗口,至少我无法.因此,解决方案是使用类别为应用程序中的每个NSWindow覆盖canBecomeKeyWindow方法.

The main problem is the way keyboard events works. Although the NSTextField (and all his superviews) receives keyboard events, he 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 no 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做出否定响应的窗口,则始终可以创建一个子类.

That makes the popover fully resposive. If you need another window which must respond NO to canBecomeKeyWindow, you can always make a subclass.

我不太了解我该怎么做.我是否只创建了这两个文件NSWindow + canBecomeKeyWindow(.h和.m),就可以了吗?因为它对我不起作用.

I don't really understand what I am supposed to do. Do I just create these two files NSWindow+canBecomeKeyWindow (.h and .m) and that will do it? Because it doesn't work for me.

我对此不确定,但是仅当我实际使用显示NSPopover的NSWindow时才有效吗?我没有使用NSWindow,如何使NSTextField可编辑?

I am not sure about this but does this only work if I am actually using an NSWindow that displays the NSPopover? I am not using a NSWindow, how can I get the NSTextField to be editable?

谢谢.

推荐答案

是的,只需将NSWindow + canBecomeKeyWindow(.h和.m)文件添加到您的项目中,它应该可以工作.我在当前正在开发的应用程序中使用了此技术,并且效果很好.确保在项目的构建阶段"的编译源"下列出了NSWindow + canBecomeKeyWindow.m.

Yes, just add the NSWindow+canBecomeKeyWindow (.h and .m) files to your project, and it should work. I'm using this technique in an app I'm currently developing, and it works fine. Make sure NSWindow+canBecomeKeyWindow.m is listed under "Compile Sources" in your project's Build Phases.

顺便说一句,在使用NSPopover显示来自NSStatusItem的窗口时,我还有其他问题.我实际上并没有在项目中尝试使用它,但是看起来很有希望.

As an aside, I'm having other issues using NSPopover to show a window from an NSStatusItem. I haven't actually tried using it in my project, but this looks promising as an alternative.

这篇关于带有NSPopover和NSTextField的NSStatusItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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