gpt4 book ai didi

objective-c - 带有 NSPopover 和 NSTextField 的 NSStatusItem

转载 作者:行者123 更新时间:2023-12-04 02:33:07 24 4
gpt4 key购买 nike

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

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

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

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)就可以了?因为它对我不起作用。

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

谢谢。

最佳答案

是的,只需将 NSWindow+canBecomeKeyWindow(.h 和 .m)文件添加到您的项目中,它应该可以工作。我在我目前正在开发的应用程序中使用了这种技术,它运行良好。确保 NSWindow+canBecomeKeyWindow.m 列在项目构建阶段的“编译源”下。

顺便说一句,我在使用 NSPopover 显示 NSStatusItem 的窗口时遇到了其他问题。我实际上并没有尝试在我的项目中使用它,但是 this看起来很有前途。

关于objective-c - 带有 NSPopover 和 NSTextField 的 NSStatusItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com