1

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];
}
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62

1 Answers1

2

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]];
Wil Shipley
  • 9,343
  • 35
  • 59
  • Just fill in the nib name for the nib with the view. In that nib the “File’s Owner” should be of type NSViewController, and its outlet to the view should be hooked up. – Wil Shipley Jan 20 '14 at 00:03
  • I have completely re-written the question, making it easier to understand what I was asking. Sorry for the confusion, maybe you can comment again? – Daniel Farrell Jan 20 '14 at 01:18
  • 1
    Cool. The big issue we have on OS X is we can’t say, “This view in this XIB is a placeholder from another XIB,” that still needs to be addressed but the AppKit team. – Wil Shipley Jan 20 '14 at 01:54
  • Thank you! I understand now. I will I was falling over myself trying to figure out why the code look horrible, the answer (as it is normally) is that I was fighting Cocoa. In the end I will just have to stick with the two controllers. By the way, I wrote InnerShadowTextFieldCell following your advice from another thread https://github.com/danieljfarrell/InnerShadowTextFieldCell – Daniel Farrell Jan 20 '14 at 02:09
  • Wil, can you clarify: if one has to use that code to construct the stack view, why have the controllers in a nib at all? – Chris Page May 05 '14 at 21:36
  • I’d rather have a different XIB for each subview in a stackView, no matter how I assemble the final object. It just seems cleaner to me. But whatever you feel works best for you is good! – Wil Shipley May 07 '14 at 07:01
  • This is a point that screwed my brain for forever. Maybe it will be better with story boards and IBDesignable and such in Xcode 6 and 10.10. It just seems like having a view controller proxy in a nib/xib should also let you tell it to magically load associated view for design from that other place. At least until the new stuff is in, we won't know for sure, but it looks like it might be there. :) – uchuugaka Aug 17 '14 at 15:27