Reputation: 16482
Where is the main window in a document-based Mac application? If you create a new project from Xcode there is no window in the MainMenu.xib. There is only a window object in the Document.xib which is used for document's window. I want to design the main window in interface builder but I don't understand where it's being created.
EDIT
I think I am encountering a bug in Xcode. Creating a document-based app with random name works as expected.
However if I create a document-based app with a name of a previous project name I am seeing a non-document window first presented and also this error in the console.
Unknown class 'AppDelegate', using 'NSObject' instead. Encountered in Interface Builder file at path /Users/[COMPUTER]/Library/Developer/Xcode/DerivedData/[PROJECT]-gohsjlddlopenfapdikaqibmvvqs/Build/Products/Debug/[PROJECT]/Contents/Resources/en.lproj/MainMenu.nib.
Upvotes: 1
Views: 896
Reputation: 12782
The basic cocoa document architecture assumes the app has one window per document and the main menu nib is owned by the app delegate.
It doesn't prevent you from subclassing or adding additional windows to any nib or controller.
If what you want is a main window and document windows, that's a common paradigm, but not in the template provided.
The simplest way to do this is add a window to main menu nib. Then you probably want it to have a way to reference all your document windows. Mail on the Mac is a good example of this, though it also allows multiple main windows.
You'll probably need a custom NSWindowController subclass to do this right.
But you can also add an NSObject subclass as your app delegate and mimic the configuration of a delegate in a non-Document Based app project template.
You'll need to go through a few steps to do this.
Create an NSObject subclass.
Make sure it declares the protocol.
In your MainMenu.xib, add an NSObject (blue box looking thing) from the library.
Set its class to your new NSObject subclass.
Click the NSApplication placeholder icon in the xib file, and in the inspector on the right, connect the delegate property to your app delegate object.
Now select your app delegate object.
Connect its window property to your window in the xib.
If there isn't one, and there probably is not, then you'll need to declare it in your app delegate class interface. (this is the same as adding a property for any button or other object, but you'll want to change it from
@property (unsafe_unretained) IBOutlet NSWindow *window;
to
@property (strong) IBOutlet NSWindow *window;
Upvotes: 2
Reputation: 126107
Document-based apps don't have a "main" window -- by definition, a document-based app is based on a UI involving one window for each document. If, when creating a new app project in Xcode, you want an app whose UI centers on a single window (like iTunes or System Preferences), uncheck "Create Document-Based Application" after choosing the Application template.
If you want to create an app that uses the document-based workflow but also has non-document-specific windows, you can create separate nibs for those windows and implement logic to show and control them someplace outside of your document class. (For example, you could create an NSWindowController
subclass for a preferences window, with accompanying nib for that window's user interface, and instantiate/show that window from a menu command implemented in your application delegate.)
Upvotes: 4