使NSView在NSPanel第一响应者没有键窗口状态 [英] Make NSView in NSPanel first responder without key window status
问题描述
有可能给NSView内部的NSPanel第一响应者状态,而不给出NSPanel键窗口状态(使主应用程序窗口辞职键)?
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)?
谢谢。
推荐答案
好了,我结束了这一个,但它需要进行大量的研究,以防其他人遇到同样的问题。首先,一些基本知识:
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
来伪造一个窗口,但不会给出窗口第一个响应者状态中包含的视图。
- It's impossible to have 2 windows actually be key at the same time
- It's possible to fake a window into thinking it's key by overriding
-isKeyWindow
but that won't give the views contained in the window first responder status.
我的情景:
在我的主应用程序窗口中包含一个 NSTableView
(原因是irrelavant)。子窗口是 NSPanel
与 NSBorderlessWindowMask
。我想给予 NSTableView
第一响应者状态,而不使面板成为关键窗口,因为它从主窗口带走焦点(并且子窗口幻象的整个点是使子窗口看起来像是主窗口的一部分)。
I added a child window containing an NSTableView
into my main application window (the reason is irrelavant). The child window was an NSPanel
with NSBorderlessWindowMask
. I wanted to give the NSTableView
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).
我尝试的第一件事是愚弄表视图,认为它是在关键窗口内覆盖 isKeyWindow
以返回 YES
。
The first thing I tried was fooling the table view into thinking that it was inside the key window by overriding isKeyWindow
to return YES
. This made the table view draw as if it were the first responder, but still did not give it first responder status.
解决方案:
所以默认情况下,NSBorderlessWindowMask不允许窗口成为键。为了使表视图第一个响应者,窗口必须是键,所以我在无边框窗口子类中覆盖 canBecomeKeyWindow
,返回 YES
。这当然是从主窗口拿走关键状态,这是我想避免的事情之一。为了解决这个问题,我将主要窗口子类化并覆盖了以下方法:
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 return YES
. 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:
- (BOOL)isMainWindow
{
return YES;
}
- (BOOL)isKeyWindow
{
return ([NSApp isActive]) ? YES : [super isKeyWindow];
}
此子类检查应用程序是否处于活动状态,如果是,返回 YES
,这样无论在你的应用程序中什么窗口是活动的,主窗口将总是表现为它仍然是键。这种给出的错觉,你可以有多个窗口是同时的键,使您可以将键窗口状态转移到另一个窗口,而不会失去它在主窗口。希望这有助于!
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!
这篇关于使NSView在NSPanel第一响应者没有键窗口状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!