使NSPanel中的NSView成为无关键窗口状态的第一响应者
是否可以在不提供 NSPanel 关键窗口状态(使主应用程序窗口退出关键)的情况下,为 NSPanel 内的 NSView 提供第一响应者状态?
谢谢。
Is it possible to give an NSView inside an NSPanel first responder status without giving the NSPanel key window status (making the main application window resign key)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我最终解决了这个问题,但是需要进行大量研究,因此我将在这里发布详细信息,以防其他人遇到同样的问题。首先,一些基础知识:
-isKeyWindow
来伪造一个窗口,使其认为它是关键,但这不会给出视图包含在窗口第一响应者状态中。我的场景:
我在主应用程序窗口中添加了一个包含
NSTableView
的子窗口(原因无关)。子窗口是一个带有NSBorderlessWindowMask
的NSPanel
。我想给NSTableView
第一响应者状态而不使面板成为关键窗口,因为它夺走了主窗口的焦点(子窗口幻象的全部目的是使子窗口看起来像它是主窗口的一部分)。我尝试的第一件事是通过覆盖
isKeyWindow
返回YES
来欺骗表视图,使其认为它位于关键窗口内。这使得表视图绘制得好像它是第一响应者一样,但仍然没有赋予它第一响应者状态。解决方案:
因此默认情况下,NSBorderlessWindowMask 不允许窗口成为关键。为了使表视图成为第一响应者,窗口必须是关键,因此我重写了无边框窗口子类中的
canBecomeKeyWindow
以返回YES
。当然,这剥夺了主窗口的关键状态,这是我想避免的事情之一。为了解决这个问题,我对我的 main 窗口进行了子类化,并覆盖了以下方法:该子类检查应用程序是否处于活动状态,如果是,则始终返回
YES
,这样就不会无论应用程序中哪个窗口处于活动状态,主窗口始终会表现得好像它仍然是关键窗口。这种方式给人一种错觉,即您可以同时让多个窗口成为关键,并使您能够将关键窗口状态转移到另一个窗口,而不会在主窗口上丢失它。希望这有帮助!Well, I ended up figuring this one out, but it took a lot of research so I'll post the details here in case anyone else runs into the same problem. First of all, a few basics:
-isKeyWindow
but that won't give the views contained in the window first responder status.My Scenario:
I added a child window containing an
NSTableView
into my main application window (the reason is irrelavant). The child window was anNSPanel
withNSBorderlessWindowMask
. I wanted to give theNSTableView
first responder status without making the panel the key window because it took away focus from the main window (and the whole point of the child window illusion was to make the child window look like it was part of the main window).The first thing I tried was fooling the table view into thinking that it was inside the key window by overriding
isKeyWindow
to returnYES
. This made the table view draw as if it were the first responder, but still did not give it first responder status.The Solution:
So by default, NSBorderlessWindowMask will not allow the window to become key. To make the table view first responder, the window had to be key so I overrode
canBecomeKeyWindow
in the borderless window subclass to returnYES
. This, of course, took away key status from the main window, which was one of the things I wanted to avoid. To fix this, I subclassed my main window and overrode the following methods:This subclass checks if the application is active, and if it is, it always returns
YES
so that no matter what window is active in your application, the main window will always behave as if it is still key. This sort of gives the illusion that you can have multiple windows be key at the same time and enables you to shift key window status to another window without losing it on your main window. Hope this helps!