4

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?

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Ram Patra
  • 16,266
  • 13
  • 66
  • 81
  • 3
    Make it lazy so you have a reference to it once’s it’s initialized. https://github.com/DanielStormApps/Fanny/blob/c3f010848ccb12d986a1b8755fe4c5cc25728484/Fanny/fanny/Menu/FNYMenuController.swift#L24 – Daniel Storm Mar 24 '20 at 16:21
  • @DanielStorm if you wish you can answer the same and I can accept it – Ram Patra Mar 24 '20 at 16:32
  • 2
    Thanks. That’s ok though. I’m on my phone so it is kind of a pain to format an answer correctly. Feel free to answer your own question and accept it when you’re able to – Daniel Storm Mar 24 '20 at 16:48

0 Answers0