如何让NSPopover的分离窗口成为模态窗口

发布于 2024-12-26 06:55:30 字数 183 浏览 0 评论 0原文

在 MAC OS X 10.7 中,Apple 引入了一个名为 NSPopover 的新类,您甚至可以拖走该弹出窗口视图以成为独立的 NSWindow。但是,我想阻止用户与主窗口交互,直到关闭分离的窗口。我怎样才能安全地做到这一点?

实际上,一个更常见(甚至更愚蠢)的问题应该是,如何防止任何用户交互,直到当前前窗口返回?我也不懂编程。

In MAC OS X 10.7, Apple introduced a new class called NSPopover and you can even drag away that popover view to become an independent NSWindow. However, I want to prevent user interaction to the main window until the detached window is closed. How can I safely do this?

Actually, a more common (and even more stupid) question should be, how to prevent any user interaction until current front window returns? I am noob to tread programming also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浮云落日 2025-01-02 06:55:30

我自己找到了解决方案。现在看起来工作正常。

为此,在将分离的窗口排序到前面并成为关键窗口之后,以下代码将使其成为模态窗口(其中 currModalSession 是我自己定义的 iVar)。

- (void)windowDidBecomeKey:(NSNotification *)notification {
    if (notification.object == detachedWindow) {
        if (!detachedWindow.isModalPanel) {
            currModalSession = [NSApp beginModalSessionForWindow:detachedWindow];
            [NSApp runModalSession:currModalSession];
        }
    }
}

此外,您必须结束已打开的每个模态会话。因此,以下代码可以完成这项工作:

- (void)windowWillClose:(NSNotification *)notification {
    if (notification.object == detachedWindow) {
        if (currModalSession) {
            [NSApp endModalSession:currModalSession];
        }
    }
}

注意: 您必须在此处使用 Modal Session 而不是 runModalForWindow,原因有两个:

  1. 否则主窗口不会立即被阻止。我还不太明白原因。一种可能的解释是:runModalForWindow 不仅会阻止用户交互,还会阻止内部通信,因此主窗口可能需要更多时间才能准备好。
  2. 如果您打算从分离的窗口运行另一个框架模态对话框(例如 NSOpenPanel),则当返回时,分离的窗口将在新的模态对话框关闭之前成为关键窗口,即 runModalForWindow 将冻结另一个要关闭的窗口。这意味着关闭的窗口不会被关闭。

I kind of found the solution myself. It looks working fine now.

To do this, after the detached window ordered to front and become key window, the following code will make it a modal window (where currModalSession is an iVar defined by myself).

- (void)windowDidBecomeKey:(NSNotification *)notification {
    if (notification.object == detachedWindow) {
        if (!detachedWindow.isModalPanel) {
            currModalSession = [NSApp beginModalSessionForWindow:detachedWindow];
            [NSApp runModalSession:currModalSession];
        }
    }
}

Also, you have to end each Modal Session you have opened. So the following code does the job:

- (void)windowWillClose:(NSNotification *)notification {
    if (notification.object == detachedWindow) {
        if (currModalSession) {
            [NSApp endModalSession:currModalSession];
        }
    }
}

Note: you have to use Modal Session here rather than runModalForWindow for two reasons:

  1. otherwise the main window won't be blocked right away. I don't quite get the reason yet. One possible explanation is: runModalForWindow will not just block user interactions but also internal communications, so main window might need more time to be ready.
  2. if you plan to run another framework modal dialog (e.g. NSOpenPanel) from the detached window, when return, the detached window will become key window before the new modal dialog close, namely runModalForWindow will freeze another to be closed window. That means to be closed window won't be closed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文