How to prevent multiple instances of the same window from opening in macOS?

对着背影说爱祢 提交于 2021-02-08 06:35:37

问题


I am making a Mac Menu Bar app and it has a Preferences option. When the user clicks on it, a Preferences window opens up that shows all the settings for the app.

I open the Preferences window like:

let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: "PreferencesWindowController") as! NSWindowController
windowController.showWindow(self)

File: AppDelegate.swift (above code is run when user clicks on "Preferences")

Now, if the user clicks twice then two Preferences windows open up which I do not want. I only want one "Preferences" window to open even if the user clicks multiple times.

I can, of course, create a boolean flag here and toggle and check before opening the window but wanted to know whether there is any API (cleaner way) to achieve this?

来源:https://stackoverflow.com/questions/60835055/how-to-prevent-multiple-instances-of-the-same-window-from-opening-in-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!