NSDocument with multiple windows -- Appkit dialog sheets 跳转到错误 window

NSDocument with multiple windows -- Appkit dialog sheets jump to the wrong window

很少使用,但 Apple 的 NSDocument 文档描述了如何为单个文档设置具有多个 windows 的 NSDocument。我正在开发一个执行此操作的数据库应用程序。这是一个打开了两个 windows 的支票簿数据库文档的示例。每个 window 显示文档的不同视图,在这种情况下,后面的视图是展开的 sheet,前面的是汇总此数据集的图表 window。此示例为一个文档显示两个 windows,但用户可以根据需要为每个文档创建任意多个 windows,每个文档以不同的方式显示相同的基础文档。

一切正常,除了,如果系统对话框 sheet 打开(另存为、打印、页面设置),大部分时间(但不是 次)对话框sheet跳转到另一个window并附加到那个window而不是当前的window,如此处所示电影。

请注意,虽然对话框 sheet 附加到包含图表的 window,但它正确打印了跨页 sheet window 中的内容。如果我按打印,就会打印出正确的内容。

对于打印,我们所有的代码都是调用 NSDocument printDocument: 方法。

[NSApplication sendActionToFirstResponder:@selector(printDocument:)];

页面设置代码也只是调用 NSDocument。

[NSApplication sendActionToFirstResponder:@selector(runPageLayout:)];

我们的代码没有自定义任何这些对话框 sheet,它们完全是原装的。

对于 Save As 命令,我们的应用程序中根本没有代码,当 option 键时,它会自动出现在菜单中被按下。

此问题出现在我们的应用程序支持的所有 macOS 版本中,从 10.9 到 10.13。也许这是一个很少见的 AppKit 错误,因为很少使用单个文档的多个 windows?

这个问题不会导致崩溃,也不会阻止用户做他们想做的事,但它明显不正确,会降低用户对程序质量的信心。

供我参考,这是 Panorama X 问题跟踪器中的 #221。

Implement/override NSDocument 属性 windowForSheet.

The value of this property may be nil, in which case the sender should present an app-modal panel. The NSDocument implementation of this property sets the value to the window of the first window controller, or [NSApp mainWindow] if there are no window controllers or if the first window controller has no window.