macOS 显示主窗口

macOS 多窗口程序打开主窗口

Posted by Yang on September 1, 2021

最近提交一个新的 mac 应用,程序是多窗口的,被拒了好几次,根据苹果返回的被拒原因,一直改了几次,都没改到点上。最后才意识到问题是这样的,用户打开了多个窗口,然后关闭了主窗口,需要能够返回或者打开主窗口,要不然不符合苹果的软件设计,仔细想想确实是这样的,只是有时候忽略了这个地方。

我截取了一段被拒的原因

Design Preamble

The user interface of your app is not consistent with the macOS Human Interface Guidelines.

Specifically, we found that when the user closes the main application window there is no menu item to re-open it.

— It would be appropriate for the app to implement a Window menu that lists the main window so it can be reopened, or provide similar functionality in another menu item. macOS Human Interface Guidelines state that “The menu bar [a]lways contains [a] Window menu”.

第一次收到这个被拒原因时,我第一反应是在关闭主窗口时,没有提醒用户保存数据就关闭了主窗口,提交版本后还是被拒。最后无意中关闭了主窗口,才发现了问题所在,接下来是解决方法。

当关闭主窗口后,点击 dock 栏的 icon,显示主窗口即可.

在 AppDelegate 中实现以下方法。

1
2
3
4
5
6
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { 
        let windows = NSApp.windows  // 所有的 NSWindow
        let homeWindow = windows[0]  // 主窗口
        homeWindow.makeKeyAndOrderFront(homeWindow)
        return true
    }