Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update second view controller in NSSplitViewController

I have a NSSplitViewController in which first viewcontroller displays a table and second viewcontroller should display a viewcontroller from a list of viewcontrollers based on the selection of table row.

enter image description here

I'm using tableViewSelectionDidChange() to get the selected row.

Now I have a bunch of viewcontrollers(storyboard ID given as row number) that I should add and remove to second viewcontroller in NSSplitViewController

How can I do that?

like image 924
unknownymouse Avatar asked Jan 04 '23 08:01

unknownymouse


1 Answers

You are on the right path. Within tableViewSelectionDidChange() you need to instantiate a new viewController from your storyboard using NSStoryboards instantiateController(withIdentifier: String) method. Then you can set this as your splitViews second view controller.

Then you need to create a splitViewItem. You can use the init method which takes a viewController for this (NSSplitViewItem(viewController:)).

Finally you have two possibilities to add the new viewController. Either you use the add/removeSplitViewItem methods or you set the splitViewItems array directly.

My words in code:

guard let splitViewController = self.parent as? NSSplitViewController,
      let viewController = self.storyboard?.instantiateController(withIdentifier: "yourIdentifier") as? NSViewController
        else { return }

let item = NSSplitViewItem(viewController: viewController)

// Method one
splitViewController.removeSplitViewItem(splitViewController.splitViewItems[1])
splitViewController.addSplitViewItem(item)

// OR method two
var items = splitViewController.splitViewItems
items[1] = item
splitViewController.splitViewItems = items
like image 134
mangerlahn Avatar answered Jan 11 '23 07:01

mangerlahn