重新显示窗口后未调用 NSWindow windowDidResignKey

发布于 2024-11-24 19:59:17 字数 1078 浏览 1 评论 0原文

我有一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌伤浅笑 2024-12-01 19:59:17

问题是,在某些情况下,调用 [attachedWindow makeKeyAndOrderFront:self] 后,自定义窗口实际上并未成为关键窗口。

我通过在重新创建窗口之前添加以下行来修复此问题:

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

在上面的代码片段的上下文中:

- (void) toggleWindowAtPoint:(NSPoint)point
{
   // Attach/detach window.
   if (!attachedWindow) 
   {
      [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
      attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
      ....

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:

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

In the context of the code snippet above:

- (void) toggleWindowAtPoint:(NSPoint)point
{
   // Attach/detach window.
   if (!attachedWindow) 
   {
      [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
      attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
      ....
一袭白衣梦中忆 2024-12-01 19:59:17

您是否尝试过在切换方法中调用 [attachedWindow makeFirstResponder:attachedWindow] ?

Have you tried calling [attachedWindow makeFirstResponder:attachedWindow] in your toggle method?

時窥 2024-12-01 19:59:17

如果您想在不使用 activateIgnoringOtherApps: 的情况下激活窗口,您应该使用带有 NSNonactivatingPanelMask 的 NSPanel:

[[CustomPanel alloc] 
        initWithContentRect: NSZeroRect 
                  styleMask: NSNonactivatingPanelMask 
                    backing: NSBackingStoreBuffered 
                      defer: NO];

If you want to activate a window without using activateIgnoringOtherApps: you should use a NSPanel with a NSNonactivatingPanelMask:

[[CustomPanel alloc] 
        initWithContentRect: NSZeroRect 
                  styleMask: NSNonactivatingPanelMask 
                    backing: NSBackingStoreBuffered 
                      defer: NO];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文