I’m stumbling over some basic concepts, that I cannot grasp. I hope, somebody can clear some things up for me, as I’ve found no resource, that would explain that. OR maybe, it’s in the bright open and I just don’t see it.
Understood so far: The MainWindow holds the menu and is therefore more or less essential. The info.plist holds the nib, that’s loaded on appstart.
Not understood so far: I am trying a single window application. I can do everything in the appDelegate (works fine so far) and I can have an additional controller, to whcih I can connect UIElements from the MainWindow to (works although fine so far). BUT: What I’d really like to do, is have a MainWIndow, that only has the menu, and a separate controller and nib (possibly even more than one of both later on), that are loaded and added subsequently.
My questions:
-
Use
NSWindowControllerorNSViewController? And why? (I’d useNSViewController) -
What, where and how to instantiate (presumably in the
didFinishLaunchingof the appDelegate?) -
How to add window or view to the one and only main-window instead of having a second, independent window (I’m not yet up for multiDocumentApps)
Thanx a lot, any thoughts appreciated!
For the sake of clarity, MainWindow is a nib file so I’ll refer to it as MainWindow.nib. It is the standard nib file name specified in the application’s Info.plist file as the nib file to be loaded when the application starts.
By default, Xcode creates a MainWindow.nib file that contains both the main menu and the main window. You’re free to delete the window from MainWindow.nib file and have another nib file to hold that window. Let’s call this other nib file MyWindow.nib.
Since you’ll have a separate nib file to hold a window, you’ll use
NSWindowController. Create a subclass ofNSWindowController, e.g.MyWindowController, which will be responsible for controlling the window stored in MyWindow.nib. This subclass will have outlets pointing to the user-interface elements in that window, and potentially other objects you define in MyWindow.nib.When doing this, it’s important that you do the following in MyWindow.nib:
Set the file’s owner to be of type
MyWindowController;Connect the
windowoutlet in file’s owner to the window object.NSViewControlleris used to manage a view, normally loaded from a nib file, and it doesn’t apply to windows.You can repeat this — window in a .nib file, subclass of
NSWindowControllerto manage that window — for as many windows as needed.Since you want a single window in your application, one option is for your application delegate to hold a reference (instance variable, declared property) to the single window controller that manages that window. Then, in
-applicationDidFinishLaunching:, instantiate said controller.For example:
and:
If you want more windows, it’s just a matter of declaring instance variables/properties to hold their corresponding controllers and instantiate the controllers like above.
Are you sure you want to add a window to the main window? If so, that’d be called an attached window. You can use the mechanism above (place the window in its own nib file, have a subclass of
NSWindowControllerto manage it, have your originalMyWindowControllerhold a reference (instance variable, declared property) to the attached window controller, and instantiate it/load the attached window nib file when appropriate) and then use-[NSWindow - addChildWindow:ordered:]to attach the secondary window to the main window.For example, considering
MyWindowControllerhas a declared propertysecondaryWindowController, in MyWindowController.m:If you want to add a view to the main window, the easiest way is to do so in the nib file directly.
If you need/want to do it programatically, you need to have a reference to the view and then add it to the main window’s content view, e.g.:
You can create
someViewprogrammatically or load it from a separate nib file. In the latter case, the procedure is much like what was described above but instead of using a subclass ofNSWindowControlleryou’d use a subclass ofNSViewController.For example, in MyWindowController.m: