Search code examples
macoscocoaswiftnswindownsapplication

how to prevent storyboard from opening a window


I am writing a simple application that loads into the status bar and when clicked it opens a popover under the status bar icon. I am using storyboards to define the UI I like to show in my popover. The thing is as soon as I instantiate the storyboard a window opens up. How can I prevent that? This is the code in my application delegate:

func applicationDidFinishLaunching(aNotification: NSNotification) {
    let mainBoard = NSStoryboard(name: "MainBoard", bundle: nil)
}

Solution

  • import Cocoa
    
    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
        var defaultWindow:NSWindow!
        func applicationDidFinishLaunching(aNotification: NSNotification) {
            defaultWindow = NSApplication.sharedApplication().windows.first as? NSWindow
            defaultWindow.close()
    
        }
        func applicationWillTerminate(aNotification: NSNotification) {
            // Insert code here to tear down your application
        }
        @IBAction func menuClick(sender: AnyObject) {
            defaultWindow.makeKeyAndOrderFront(nil)
        }
    }
    

    update: Xcode 7.1.1 • Swift 2.1

    NSApplication.sharedApplication().windows.first?.close()