Domanda

So, create a custom NSViewController subclass with xib/nib. Then in the app's main nib, add an NSViewController object from the object library, set the class to the custom view controller class. Create IBOutlet in the app delegate for the custom view controller.

Add an NSView object to the window. Set the view controller's view to the NSView in the window.

When I build & run, the view is the generic view in the main nib, not the one from the view controller's nib...

What else is needed to get this process to work as I think it should? I would think this should populate the view (and any subviews) from the custom view controller's nib.

I know how to do this in code, but it sure seems like boiler plate. (especially with the addition of auto layout constraints.)

Please tell me I am missing something stupidly obvious.

È stato utile?

Soluzione

When you connect the view outlet of the NSViewController to the empty generic container view, it's replacing the actual view loaded from the NIB with the empty view at runtime.

Unfortunately there's no way to do what you're asking purely by using Interface Builder and outlets. The easiest alternative method:

Add the NSViewController object to the main XIB file and set the Nib Name and Nib Bundle in the Attributes inspector, but don't set the view outlet to the empty container view.

Add properties to your application delegate (or other controller class exposed in the XIB) to create IBOutlets to the view controller and container view. Example:

@property (nonatomic, strong) IBOutlet NSViewController *viewController; // view controller in XIB
@property (nonatomic, weak) IBOutlet NSView *containerView;

Connect these outlets in the XIB to the view controller and container view. Now, at runtime, add the view controller's view as a subview of the container view:

NSView *realView = self.viewController.view;
realView.frame = self.containerView.bounds;
// Also configure autoresizing behaviour for realView using either autoresizing masks 
// or layout constraints so that realView resizes with its parent container
[self.containerView addSubview:realView];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top