How does Xcode decide which XIB to show first
Asked Answered
H

2

7

When I create a new cocoa project in Xcode, it creates a MainMenu.xib file with a Window. I would like to separate the window into a MainWindow.xib and have that show up as the main window - how do I do that?

If I create a 'document' based application - I see two xibs (MainMenu.xib and MyDocument.xib) and I notice that MyDocument.xib gets displayed right away - but I don't understand where this is identified. There doesn't seem to be explicit code and I'm not sure where to look in the plists.

If I create a new Document, how can I make it the primary window that shows up when I start the app?

Hoyos answered 8/3, 2011 at 20:18 Comment(2)
Xcode decides no such thing, because Xcode does not show xibs (other than as items in your group tree). Nibs cannot be shown anyway; they are archives of objects. See developer.apple.com/library/mac/documentation/Cocoa/Conceptual/… and developer.apple.com/library/mac/documentation/Cocoa/Conceptual/… .Neela
Thanks Peter. I don't doubt you points - but I think you missed the underlying question. How and what identifies which windows startup right away? The application is an NSApplication. The Main nib is identified as MainMenu ... and then I'm lost. What is special about "MyDocument" that causes the magic to open it up right away. How can I open 5 of these? I am looking for the answer to what actual configuration determines what displays when the app starts. The docs you refer to are great for XIBs ... but I can't find the section that walks me through the properties identifying startup windows.Hoyos
S
10

in <YourProjectName>-Info.plist you'll find the key "Main nib file base name", with the value of the first xib to load usually "MainMenu".

Also you'll find "Document types" -> "Item 0" -> "Cocoa NSDocument Class" and there the value "MyDocument". In the class "MyDocument" there is a method - (NSString *)windowNibName. This return the name of the first xib to load.

Schoolbook answered 8/3, 2011 at 20:25 Comment(4)
Thanks Viking. So, when I look at the "Main nib file base name" ... it says "MainMenu". I know I need a menu - so changing this won't help me eh?Hoyos
There are three different entries in Document types: (Binary, SQLite and XML). Each of them has "Cocoa NSDocument Class = MyDocument". What happens if each of them use a different class? Which one would get loaded automatically? I see three things there but it isn't clear which one is actually loaded first.Hoyos
Also - what if I don't have a Document based application? Let's say I create a basic app with Core Data. The app has a MainMenu.xib which has both a menu and a MainWindow. I'd like separate out MainWindow and have that automatically loaded and displayed when the app starts - how do I designate that in Interface Builder?Hoyos
I just noticed that there is no AppDelegate in a document based app - but there IS one created otherwise. Is an app delegate required in a non Document based application in which case, I already know how to create a XIB that connects the app delegate to the application and contains a window ... but does that override whatever magic is automatically happening in a Document based application? Can I somehow use some of that magic in a non-Document based app or do I want to go the App Delegate route?Hoyos
K
3

Window management is your responsibility. Don't rely on the frameworks to display windows in any order, as that order is undefined.

Firstly, make sure that the Visible at Launch checkbox is not checked for your windows in Interface Builder. If it is, you won't be able to control window display effectively. It's on by default.

Next, use an NSWindowController subclass for each window nib that you want to load. Then, in your main application controller you can control when those NSWindowController objects are instantiated and when to call their -showWindow methods.

In your example case, you'd probably instantiate the NSWindowControllerfor your MainWindow.xib in the ‑applicationDidFinishLaunching: method in your application delegate.

Katleen answered 9/3, 2011 at 0:40 Comment(8)
Why was this answer downvoted? Please leave a comment if you downvote, thanks.Katleen
Yep - this is exactly what I am doing - instantiating an NSWindowController in applicationDidFinishLaunching. But, my question is really regarding Interface Builder. There must be a very specific way that it decides what the initial window is. I'm trying to find that out. I see MainMenu.xib ... and I guess that the default Window in there get's displayed since MainMenu.xib is identified as the "Main nib base name". But I get confused when creating a Document based application. Where does the template identify the specific Window to display?Hoyos
I'm really trying to learn more about IB here - not asking for best practices per se ... although, to your point, maybe it isn't advisable to use Interface Builder to instantiate the initial window. At any rate, I'd just like to understand exactly how IB is doing it.Hoyos
If you open MainMenu.xib in IB and look at the attributes inspector for the Window, you'll notice that Visible at Launch is checked. This is why the window appears when you launch the app. MainMenu.xib is loaded by default because it's specified as the main nib in your app's Info.plist file.Katleen
Hi Rob. Thats sounds intuitive - but unfortunately, it isn't true. Xcode version 3.2.5, File >> Create New Project, Cocoa Application, [x] Create document based application, [x] Use Core Data for Storage, Choose, Give it a name, save it anywhere. You will find two XIBs (MyDocument.xib and MainMenu.xib). MainMenu.xib does not have any option to select Visible at Launch. The checkbox doesn't exist on the Attributes/Behavior tab. Open MyDocument.xib and 'Visible at Launch' exists in Attributes/Behavior ... but it is NOT checked. BOTH of these XIBs display if you compile and run the app.Hoyos
And yep - I do understand why MainMenu.xib is loaded and displayed (the NSMainNibFile property) - but my question is really about MyDocument.xib and how it gets displayed automatically. "If I create a 'document' based application - I see two xibs (MainMenu.xib and MyDocument.xib) and I notice that MyDocument.xib gets displayed right away - but I don't understand where this is identified."Hoyos
I think you're confusing regular window-based xib files with an app using the NSDocument architecture. NSDocument-based apps will display an untitled document window by default unless you use the NSApplication delegate method ‑applicationShouldOpenUntitledFile: to prevent that. The document architecture works very differently to a plain app. BTW, when I tested it, MainMenu.xib does not contain a window in a document-based app.Katleen
Yep - I think you're correct. It is the NSDocument architecture that is doing something unique. Amongst other things, I believe the (unseen) NSDocumentController is instantiating the 'first' of the three docs and that exist, etc. Thanks for replying Rob!Hoyos

© 2022 - 2024 — McMap. All rights reserved.