Xcode OS X Storyboards: How do I get a reference to the top level "window content" view controller?

2.8k views Asked by At

I have a basic Cocoa app to try out Swift and Storyboards.

The app consists of 3 main views that are managed by an NSTabViewController, which again is linked to the main window's "window content" relationship.

I added an NSSegmentedControl to the window's toolbar to switch between the 3 tab views.

In the main window controller I added the action method for the toolbar item to switch between the tabs, but I can't figure out how to get a reference to the tab view controller to actually switch tabs.

I tried adding my action method to the tab view controller, but that does not work, because the tab view controller might not be in the responder chain if it contains an empty view (is this a bug?).

As a workaround, I overrode viewDidAppear() in the tab view controller. There I get a handle to the view.window.windowController and set a reference to "self" in an instance variable. This works, but does not seem right. There should be a way to get from the window controller to it's content view controller...

The window controller and tab view controllers live in different scenes, to I can't connect them through IBOutlet. Also, none of the segue methods are called on the window controller either for the "window content" relationship.

Storyboard

2

There are 2 answers

3
The Cappy On

I'm mainly an OS X guy, and I'm new to StoryBoards myself, but I think what you need to do is:

  1. Write your

    -(IBAction)someResponse:(id)sender {}

method in the view controller that plans to handle the action.

  1. Wire the Control so that it sends its action to its FirstResponder. If your view controller is in the responder chain, it'll receive it.
0
mna On

I know I'm incredibly late, and maybe it didn't exist when that question was posted, but for anyone landing here, the NSWindowController has a contentViewController: NSViewController? property (added in macOS 10.10, so around the end of 2014 I think).