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

1.1k Views Asked by At

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?

0

There are 0 best solutions below