从函数中打开 NSViewController。菜单栏应用

Open NSViewController from function. Menu Bar App

我正在尝试从按下 NSMenuItem 时启动的函数打开 NSViewController。

func settings (sender: NSStatusBarButton) {

    //open NSViewController here?

}

假设包含要打开的 window 的 NSWindowController 具有 Storyboard ID Settings 并且 NSViewController 具有自定义 class SettingsController 禁用关联 window 的选项 Visible At Launch 并使用此代码

var windowController : NSWindowController!

let mainStoryBoard = NSStoryboard(name: "Main", bundle: nil)
windowController = mainStoryBoard.instantiateController(withIdentifier: "Settings") as! NSWindowController
let settingsController = windowController.window!.contentViewController as! SettingsController
// make initial settings before showing the window
windowController.showWindow(self)