重新显示窗口后未调用 NSWindow windowDidResignKey
我有一个自定义 NSWindow 子类,用户可以通过单击按钮来切换其显示。我还希望当窗口放弃关键状态时窗口消失(例如,通过用户在窗口外部单击)。
我有一个实现 windowDidResignKey:
的委托,但我发现该委托方法仅在窗口第一次退出键时被调用。
以下是我切换窗口显示的方法(通过用户操作或 windowDidResignKey):
- (void) toggleWindowAtPoint:(NSPoint)point
{
// Attach/detach window.
if (!attachedWindow)
{
attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
attachedWindow.delegate = self;
[attachedWindow setLevel:NSMainMenuWindowLevel+1]; // show window in front of all other apps on desktop
[attachedWindow makeKeyAndOrderFront:self];
}
else
{
attachedWindow.delegate = nil;
[attachedWindow orderOut:self];
[attachedWindow release];
attachedWindow = nil;
}
}
这是我对 windowDidResignKey 的实现:
- (void) windowDidResignKey:(NSNotification *)note
{
[self toggleWindowAtPoint:NSMakePoint(0, 0)];
}
我发现第一次显示自定义窗口时,会调用 windowDidResignKey:
。此后每次重新显示自定义窗口时,都不会调用 windowDidResignKey:
。
I have a custom NSWindow subclass that the user can toggle the display of with the click of a button. I'd also like the window to disappear when the window resigns key status (e.g. by the user clicking outside the window).
I have a delegate that implements windowDidResignKey:
but I find that this delegate method is only invoked the first time the window resigns key.
Here's how I toggle the display of the window (via user action or windowDidResignKey):
- (void) toggleWindowAtPoint:(NSPoint)point
{
// Attach/detach window.
if (!attachedWindow)
{
attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
attachedWindow.delegate = self;
[attachedWindow setLevel:NSMainMenuWindowLevel+1]; // show window in front of all other apps on desktop
[attachedWindow makeKeyAndOrderFront:self];
}
else
{
attachedWindow.delegate = nil;
[attachedWindow orderOut:self];
[attachedWindow release];
attachedWindow = nil;
}
}
Here's my implementation of windowDidResignKey:
- (void) windowDidResignKey:(NSNotification *)note
{
[self toggleWindowAtPoint:NSMakePoint(0, 0)];
}
I'm finding that the first time the custom window is displayed, windowDidResignKey:
gets called. Every time the custom window is re-displayed after that, windowDidResignKey:
is not getting invoked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,在某些情况下,调用
[attachedWindow makeKeyAndOrderFront:self]
后,自定义窗口实际上并未成为关键窗口。我通过在重新创建窗口之前添加以下行来修复此问题:
在上面的代码片段的上下文中:
The issue was that in some cases, the custom window was not actually becoming the key window after calling
[attachedWindow makeKeyAndOrderFront:self]
.I fixed this by adding the following line before re-creating the window:
In the context of the code snippet above:
您是否尝试过在切换方法中调用 [attachedWindow makeFirstResponder:attachedWindow] ?
Have you tried calling [attachedWindow makeFirstResponder:attachedWindow] in your toggle method?
如果您想在不使用
activateIgnoringOtherApps:
的情况下激活窗口,您应该使用带有 NSNonactivatingPanelMask 的 NSPanel:If you want to activate a window without using
activateIgnoringOtherApps:
you should use a NSPanel with a NSNonactivatingPanelMask: