I have a simple project which consists of a simple window with one view coming from a NSViewController (see https://i.sstatic.net/UAn6L.png )
The view is managed (+ linked, see screenshot) to my custom view controller object.
I dragged a ViewController Object into my MainMenu.xib, connected the view outlet and set the Controller-Class + Nib-Name.
When I launch the app my custom view won't show up in the window ("awakeFromNib" gets called in my custom NSViewController subclass!).
See Screenshot for IB details -> https://i.sstatic.net/UAn6L.png
Any ideas what I'm missing ?
I'm presuming you have a separate Nib named CustomImageView
that is not pictured (the one created automatically by Xcode). In that case you would add your controls and drawing code to the view in that separate Nib. To actually add that view controller's view to the window, you'll have to do it programmatically by making an IBOutlet connection from your AppDelegate to the view controller, and adding the following code when you want to add the view: (typically in applicationDidFinishLaunching:
)
[viewController.view setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
[viewController.view setFrame:NSInsetRect([window.contentView bounds], 20, 20)];
[window.contentView addSubview:viewController.view];
That will place it inset and centered like you have in the screenshot. The problem is that by creating the view
connection in your MainMenu Nib you are overriding the connection in the separate, CustomImageView
Nib. You can delete the Custom View object in your window... you'll have to add you custom view programmatically. It would be nice if it worked that way, but it doesn't.
EDIT:
It's because of the order that the nibs are unarchived. First CustomImageView is instantiated and all its connections are made. At this point the view
property is correctly set. Then MainMenu hooks up the view
property again, overriding the view
property in the separate Nib. Now the view property is incorrectly set. That's normal behavior. If you're using separate nib you have to leave that unconnected and add the view controller's view in code.
An alternative would be to not use a separate Nib. Just delete it from your project, set up your view-in-window and leave your connection as-is. Now you can have a separate view controller managing it, without having to add any code. You really only want a separate Nib if the view is very complex, or if you want to load it lazily as needed, like if the user could select different views for the main window.