What I do is declare a global var (in a singleton in fact) to hold the windowController.
myWindowController is of a subClass of NSWindowController, with its own nib.
In the subClass MyWindowController, I have several init:
class MyWindowController: NSWindowController, NSWindowDelegate {
override init(window: NSWindow!) {
// initialize what you need
super.init(window: window)
}
required init?(coder: (NSCoder?)) {
// initialize what you need
super.init(coder: coder!)
}
convenience init(someTag: Int) { // Need a parameter to differentiate from basic init()
self.init(windowNibName: NSNib.Name(rawValue: "MyNibName")) // NibName of the window
// initialize what you need
}
}
The global var is initialized (in singleton) with:
myWindowController = MyWindowController(someTag: 0) // in fact GlobalData.sharedGlobal.myWindowController =
Then the IBAction of menuItem to open the window is simply:
@IBAction fileprivate func showMyWindow(_ sender: NSMenuItem) {
myWindowController!.showWindow(self) // in fact, GlobalData.sharedGlobal.myWindowController!.showWindow(self)
}
Then for the menuItems to change the view in the window:
@IBAction fileprivate func loadView1(_ sender: NSMenuItem) {
let storyboard: NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
let myViewController = storyboard.instantiateController(withIdentifier: "FirstView") as? FirstViewController
self.view.window?.contentViewController = myViewController
}
@IBAction fileprivate func loadView2(_ sender: NSMenuItem) {
let storyboard: NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
let myViewController = storyboard.instantiateController(withIdentifier: "SecondView") as? SecondViewController
self.view.window?.contentViewController = myViewController
}
Note: I did not test yet, but that should work.