Switching NSViewControllers from NSWindowController

This has taken quite some time to crack as there was not that much available information online. 

I wanted a very simple way to change the NSViewController of a window on the fly by using code. 

Screen Shot 2015 12 07 at 18 41 33

The solution I ended up with is quite easy, on the NSWindowController I load the default NSViewController by typing in windowDidLoad():

let viewController = storyboard?.instantiateControllerWithIdentifier(“nsviewcontroller1″) as! NSViewController

        self.window?.contentViewController = viewController

Afterwards, when I want to change NSViewController1 by NSViewController2 i just have a button in my toolbar linked to the following action:

@IBAction func showController2(sender: NSToolbarItem){

        let viewController = storyboard?.instantiateControllerWithIdentifier(nsviewcontroller2) as! NSViewController

        self.window?.contentViewController = viewController

    }

That’s about it, extremely simple, but took me quite a bit to develop. 

As always if you have any feedback I’m at @MarcMasVi

 

Marc