View Controller for NSTabViewItem

I am using storyboards for a Mac OS X programs and have a Tab View Controller. When the Tab View Controller is segued onto to screen I would like to do some setup on each of the view controllers for the tabs.


The documentation for the NSTabViewController states: "Each tab is represented by an NSTabViewItem object, which contains the name of the tab and stores a pointer to the child view controller that manages the tab’s content."


This is exactly what I want - "a pointer to the child view controller that manages the tab’s content." Great!


But no so great -- there is no variable or method in NSTabViewItem that gives me a pointer to a view controller!! I find this very confusing. I have scanned the documentation of NSTabViewItem from top to bottom, plus looked that the header file. There is no mention of a "pointer to the child view controller" anywhere!


The documentation clearly states that this pointer is there - but I can't find it. Am I missing something obvious?


Thanks.

Answered by Ken Thomases in 25330022

It seems the documentation (even the prerelease 10.11 docs) is not complete for NSTabViewItem. Always check the headers, too. There, you will find:

/*! The view controller wrapped by the tab view item. This property must be set if the tab view item will be added to an NSTabViewController, but can also be used if the tab view item is added to an NSTabView.
* If this is set, the tab view item will forward \c -view calls onto the viewController. Setting a viewController will also set the following properties on the tab view item: \c -identifier from the address of the viewController, \c -label from the viewController's title, and \c -image based on the classname as the view controller.
* An image named "ViewControllerClassName-TabViewItem" will be searched for first, followed by "ViewControllerClassName". It will search first using +[NSImage imageNamed:], then in \c viewController.nibBundle, and lastly in the bundle containing the view controller's class.
* As defined by: -[NSImage imageNamed:imageName], -[viewController.nibBundle imageForResource:imageName], -[[NSBundle bundleForClass:[viewController class]] imageForResource:imageName]. One pass with imageName as [NSStringFromClass([viewController class]) stringByAppendingString:@"-TabViewItem"], followed by imageName as NSStringFromClass([viewController class]).
*/
@property (strong) NSViewController *viewController NS_AVAILABLE_MAC(10_10);
Accepted Answer

It seems the documentation (even the prerelease 10.11 docs) is not complete for NSTabViewItem. Always check the headers, too. There, you will find:

/*! The view controller wrapped by the tab view item. This property must be set if the tab view item will be added to an NSTabViewController, but can also be used if the tab view item is added to an NSTabView.
* If this is set, the tab view item will forward \c -view calls onto the viewController. Setting a viewController will also set the following properties on the tab view item: \c -identifier from the address of the viewController, \c -label from the viewController's title, and \c -image based on the classname as the view controller.
* An image named "ViewControllerClassName-TabViewItem" will be searched for first, followed by "ViewControllerClassName". It will search first using +[NSImage imageNamed:], then in \c viewController.nibBundle, and lastly in the bundle containing the view controller's class.
* As defined by: -[NSImage imageNamed:imageName], -[viewController.nibBundle imageForResource:imageName], -[[NSBundle bundleForClass:[viewController class]] imageForResource:imageName]. One pass with imageName as [NSStringFromClass([viewController class]) stringByAppendingString:@"-TabViewItem"], followed by imageName as NSStringFromClass([viewController class]).
*/
@property (strong) NSViewController *viewController NS_AVAILABLE_MAC(10_10);

Thank-you Ken.


This is *exactly* what I was looking for. I had looked at a header file - but I think it was an older version. I just did a quick test in Xcode and this property works as promised 🙂

View Controller for NSTabViewItem
 
 
Q