How to make a modular NSViewController based disclosure view where all disclosed views are contained in their own NIB?

StackOverflow https://stackoverflow.com/questions/21214781

Question

In real apps the view hierarchy can be complex at it really helps to be able to put different views in different nibs. I am following InfoBarStackView example project they give a really nice example of how to use the new NSStackView class which hosts different views. They make a DisclosureViewController which is responsible for hosting a content view changing the it's size so that is can go from a open to closed state.

Here is a simplified example. What we have are two separate nibs:

  1. DisclosureViewController Disclosure view controller
  2. ContentViewController Content view controller

What is the simplest way to load the content view inside the placeholder view of the disclosure view? Is it possible to do this only in IB only?

Currently my AppDelegate has a lot of redundancy because it need to hold references to both view controllers. I wondering if there is a way of simplifying the situation? For this simple example, the AppDelegate would load from the two different nibs using code like this,

// In AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [_disclosureView1.view replaceSubview:_disclosureView1.placeholder with:_contentView1.view];
    [(NSView*)_window.contentView addSubview:_disclosureView1.view];
}
Was it helpful?

Solution

You can have NSViewControllers in your main XIB corresponding the the views you want in your NSStackView — in XIB’s inspector you can set the name of the other XIBs they should load to get their ‘view’s.

Assuming you had IBOutlets onto these viewControllers in your main XIB, it’d be as easy as calling:

 /* load the views into the stack view */
_stackView = [NSStackView stackViewWithViews:@[_viewControllerOutlet1.view, _viewControllerOutlet2.view, _viewControllerOutlet3.view]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top