问题
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?
回答1:
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.
回答2:
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 NSWindowController
for your MainWindow.xib
in the ‑applicationDidFinishLaunching:
method in your application delegate.
来源:https://stackoverflow.com/questions/5237833/how-does-xcode-decide-which-xib-to-show-first