views:

222

answers:

1

I'm developing a Cocoa desktop application that uses a source list in the style of iTunes: different elements in the source list cause the main content area to display different views.

The content area is entirely filled with a (vertical) NSSplitView; on the left is an NSOutlineView source list. When the user selects an item on the left, the relevant view appears on the right side of the splitter.

I can make it work well enough by putting everything in one NIB file and putting a borderless NSTabView to the right of the splitter; to switch views, I just have to change the selected tab. But putting all the views in one NIB is bad practice, so I'm trying to move each of the subviews into their own NIB files.

I have a pretty good idea of most of this process — I've created an NSViewController subclass for each of these views (EntityDetailViewController, GroupDetailViewController, and so on), set the File's Owner of each new NIB to the relevant controller class, set the view connection in each NIB, and reworked the bindings. What I don't know is how to actually change which subview is being shown on screen.

I've tried using the default generic NSView on the right side and sending it addSubview: messages; I've tried connecting to it as the first subview and calling

NSView *newSubview = /* get subview from the new subview controller */
[[subview superview] replaceSubview:subview with:newSubview];
[self setSubview:newSubview];

But everything just leaves the space blank. How do I display a subview loaded from a separate NIB?

A: 

Your general approach seems sound. Have you verified that the newSubview from the loaded view controller is non-nil? Does the frame of newSubview look correct before and after it is added to the view hierarchy? Lastly, at what point are you using the replaceSubview: code? It should be in awakeFromNib or later.

Adam Preble