收录日期:2020/10/31 21:36:00 时间:2010-09-07 18:26:48 标签:objective-c,cocoa,interface-builder

I have some UI elements that will share common parts with other UI elements. They correspond to model objects in an inheritance hierarchy.

As an example let's say all Layers can be toggled on/off. All WaveLayers are Layers which additionally define a .wav filename. And there is a specific concrete subclass of WaveLayer called GroovyLayer which requires additional parameters.

Visually I'd like to have a long panel where the top section holds the general Layer UI, the next section holds the WaveLayer-specific UI, and the last section holds the GroovyLayer-specific UI. There may be like 10 or so concrete subclasses, each which share the same top sections corresponding to Layer and WaveLayer.

How should I set this up in InterfaceBuilder? Do I start at the top (with a placeholder for the subclass stuff) or at the bottom (with placeholders for the parent classes' stuff)? How should I go about setting up these placeholders in InterfaceBuilder and substituting in the right NSViews at the right times?

I've got this sort of thing to work by using a blank "custom view" in IB and tying it to an outlet in my controller class, and then substituting that view with another view tied to my controller from elsewhere in another NIB, but that seems unelegant, as my controller then carries both "old view" and "new view" members...

Lay out the views however you wish. Then select the custom view that represents your subclass and set its class name in the inspector.

As to whether to load the views from separate xib files, I think that's overkill here. From your description these views are always visible together, so you gain nothing but complexity loading them from individual files.

The trick you seemed to be missing was setting the class name of the custom view to that of your subclass.