如何防止故事板打开一个窗口

时间:2015-04-12 01:44:06

标签: macos cocoa swift nswindow nsapplication

我正在编写一个加载到状态栏的简单应用程序,单击它时会在状态栏图标下打开一个弹出窗口。我正在使用故事板来定义我想在我的popover中显示的UI。事情就是我在实例化故事板时打开一个窗口。我怎么能防止这种情况?这是我的应用程序委托中的代码:

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

1 个答案:

答案 0 :(得分:4)

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)
    }
}

更新: Xcode 7.1.1•Swift 2.1

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