How does Xcode install a document-based application?

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

  • A shared application instance has been created.
  • The main nib file is viewed from the list of application properties, so the application knows which nib to load.
  • a run loop starts.

This is good and makes sense for a single windowed application, but I'm confused about what xcode does when creating a document-based application.

In this case, there are two nib files; the first contains the application menu, and the second contains a window that represents the NSDocument subclass. when the application starts, a new document window opens.

Based on my understanding of how the application works above, I do not understand how my application knows how to open a document window after the nib menu has been viewed from the property list. There is no code created for this, as far as I can see (except for the windowNibName method, but where is it called from?)

Can someone tell me what xcode does differently so that the application finds out that it is based on a document and so you need to open a document window?

Update:

I am trying to understand how Xcode knows how to do something different if my application is configured as a document-based application and not a single window. To my knowledge, there is no setting to indicate this, and Xcode does not seem to generate any code to give this other behavior.

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

  • _NSApplication_has applicationOpensUntitledFile delegate method that is called by the application delegate.
  • NSDocumentController is set as the default application delegate, and the default implementation looks for CFBundledTypeInfo to determine whether the document is documented or not, and responds as appropriate for the application (IE YES for document-based applications and NO for single-window applications).
  • In most cases, when a single application window is created, the application delegate is replaced with a custom AppController in any case, which usually does not contain the definition of the applicationOpenUntitledFile method, since it is not suitable for the type of application.

We hope that any Cocoa expert can confirm whether I understood correctly or if I bark the wrong tree.

+8
xcode cocoa
source share
3 answers

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

  • Subclass NSDocument
  • New xib file for this document besides MainMenu.xib
  • A CFBundleDocumentTypes entry in Info.plist that tells the application about your subclass of NSDocument

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

For more information, read the document-based Project Template and the rest of the document -based application guide.

+5
source share

I accept your right. If you are creating an application based on a non-standard document, add document type information in -Info.plist and set the NSApplication delegate to main.m as follows

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

The behavior seems to match the application template based on the default document.

0
source share

No, your assumption is wrong, look at the implementation of the GNUstep version, in the NSApplication finishLaunching method:

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

Thus, it automatically creates an instance of NSDocumentController.

0
source share

All Articles