Swift: Switch between NSViewController inside Container View / NSView

Just note that in order for this to work you have to add storyboard identifiers to your view controllers, which can by going to your storyboard then selecting the Identity Inspector in the right hand pane and then entering the Storyboard ID in the Identity subcategory.

Then this implementation of ViewController would achieve what you are looking for.

import Cocoa

class ViewController: NSViewController {

    // link to the NSView Container
    @IBOutlet weak var container : NSView!

    var vc1 : ViewController1!
    var vc2 : ViewController2!

    var vc1Active : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Make sure to set your storyboard identiefiers on ViewController1 and ViewController2
        vc1 = NSStoryboard(name: "name", bundle: nil).instantiateController(withIdentifier: "ViewController1") as! ViewController1
        vc2 = NSStoryboard(name: "name", bundle: nil).instantiateController(withIdentifier: "ViewController2") as! ViewController2

        self.addChild(vc1)
        self.addChild(vc2)
        vc1.view.frame = self.container.bounds
        self.container.addSubview(vc1.view)
        vc1Active = true

    }

    // You can link this action to both buttons
    @IBAction func switchViews(sender: NSButton) {

        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }

        if vc1Active == true {

            vc1Active = false
            vc2.view.frame = self.container.bounds
            self.container.addSubview(vc2.view)

        } else {

            vc1Active = true
            vc1.view.frame = self.container.bounds
            self.container.addSubview(vc1.view)
        }

    }
}

maybe this is a late answer but I will post my solution anyways. Hope it helps someone.

I embedded NSTabViewController in ContainerView. Then, in order not to see tabs on the top I did this:

  • go to NSTabViewController in storyboard

  • in Attributes inspector change style to be Unspecified

  • then click on TabView in Tab Bar View Controller, and set style to be "tabless":

    enter image description here

After this you need to:

  • store tabViewController reference to mainViewController in order to select tabs from code
  • add a button to mainViewController (where your container is) with which you will change tabs in tabViewController.

You do this by storing the reference to tabViewController when overriding prepare for segue function. Here is my code:

first add property to the mainViewController

private weak var tabViewController: NSTabViewController?

then override this function and keep the reference to tabViewController:

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
            guard let tabViewController = segue.destinationController
                as? NSTabViewController else { return }

            **self.tabViewController = tabViewController as? NSTabViewController**

        }

After this you will have reference to tabViewController all set up. Next (last) thing you have to do is make an action for button in order to move to first (or second) view controller, like this:

@IBAction func changeToSecondTab(_ sender: Any) {
        self.tabViewController?.selectedTabViewItemIndex = 0 // or 1 for second VC 
    } 

All the best!