Search code examples
macosswiftcocoastoryboardstatusbar

Loading a storyboard from menuitem in appdata


This must be much simpler than I'm making it out to be.

I have a Main.storyboard that launches when app loads up. I have a system menu where one of the menu items needs to load the storyboard dialog.

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application

    statusBarItem = statusBar.statusItemWithLength(-1)
    statusBarItem.menu = menu
    statusBarItem.title = "WP"


    var options = NSMenuItem(title: "Options ...", action: nil, keyEquivalent: "")
    var quit = NSMenuItem(title: "Quit", action: Selector("terminate:"), keyEquivalent: "q")

    menu.addItem(options)
    menu.addItem(quit)

}

The options variable "action" needs to load the storyboard. Do I have access to it in the AppDelegate?


Solution

  • If I understand it correctly, it looks like your app is entirely a status bar app (with no window that appears when it launches?).

    So basically you'll want to instantiate and then bring up the window. Maybe something like this?

    in your "applicationDidFinishLaunching" function:

        var menuItem : NSMenuItem = NSMenuItem()
        menuItem.title = "Options ..."
        menuItem.action = Selector("bringUpOptionsWindow:")
        menuItem.target = self
        menuItem.keyEquivalent = ""
        menuItem.enabled = true
        menu.addItem(menuItem)
    
    // ...
    // and then later on
    // ...
    
    func bringUpOptionsWindow(sender : AnyObject)
    {
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
    
        let myController = storyboard.instantiateControllerWithIdentifier("MyOptionsWindowController") as! NSWindowController
    
        myController.showWindow(sender)
    }