Benjamin Gale
Benjamin Gale

Reputation: 13177

How does Xcode setup a document based application?

I am learning Cocoa and my understanding from reading the documentation is that when an application starts the following happens:

  1. A shared application instance is created.
  2. The main nib file is looked up from the applications property list so that the application knows which nib to load.
  3. the run loop is started.

This is fine and makes sense for s single windowed application however I am confused by what xcode does when a document based application is created.

In this case there are two nib files; the first contains the application menu and the second contains the window which represents the NSDocument subclass. when I run the application a new document window is opened automatically.

Based on my understanding of how the application works outlined above I don't understand how my application knows to open the document window once the menu nib has been looked up from the property list. There is no code generated to do this as far as I can see (except for the windowNibName method but where is this called from?)

Can anyone tell me what xcode does differently so that the application knows that it is document based and therefore needs to open a document window?

Update:

What I am trying to understand is how Xcode knows how to do something different if my application is set up as a document based application rather than a single window application. As far as I am aware there is no setting to specify this and Xcode doesn't appear to generate any code to give this different behaviour.

From reading the documents over the last couple of days I think I know how this works but am not sure:

  1. _NSApplication_has a delegate method applicationOpensUntitledFile which is called by the applications delegate.
  2. NSDocumentController is set as the applications delegate by default and the default implementation looks for the presence of the CFBundledTypeInfo to determine if the document is document based or not and responds as is appropriate for the application (I.E. YES for document based application and NO for single window applications).
  3. The majority of the time when a single window application is created the application delegate is replaced by a custom AppController anyway which usually wont contain a definition for the applicationOpenUntitledFile method as it is not appropriate for the type of application.

Hopefully any Cocoa experts can confirm if my understanding is correct or if I am barking up the wrong tree.

Upvotes: 8

Views: 3810

Answers (3)

freedostudio
freedostudio

Reputation: 1

No, your assumption is not right, look at the implementation of GNUstep version, in the NSApplication's finishLaunching method:

NSDocumentController    *sdc;
sdc = [NSDocumentController sharedDocumentController];
if ([[sdc documentClassNames] count] > 0)
{
  didAutoreopen = [sdc _reopenAutosavedDocuments];
}

So it create a instance of NSDocumentController automatically.

Upvotes: 0

yageek
yageek

Reputation: 4455

I assume your right. If you create a non based document application, add the document types informations in the -Info.plist and set the delegate of NSApplication in the main.m as following

int main(int argc, const char * argv[])
{
    [[NSApplication sharedApplication] setDelegate:[NSDocumentController sharedDocumentController]];
    [[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:NSApp topLevelObjects:nil];
    [NSApp run];
}

The behaviour seems to be the same as the the default Document-Based Application template.

Upvotes: 0

jtbandes
jtbandes

Reputation: 118681

When you create a document-based application, you get a few things:

  • A subclass of NSDocument
  • A new xib file for this document, in addition to MainMenu.xib
  • A CFBundleDocumentTypes entry in Info.plist, which tells the app about your NSDocument subclass

When your app opens, the shared NSDocumentController will create a new untitled document using the CFBundleDocumentTypes information.

For more information, read The Document-Based Application Project Template and the rest of the document-based applications guide.

Upvotes: 5

Related Questions