简体   繁体   中英

How can I create NSDocument (subclass) instances for a viewer-oriented app?

I'm trying out Cocoa (on OS X Mavericks) with a NSDocument-oriented app with a (planned) WebKit control. I noticed that the "New" menu command is disabled if all the document types are Viewer or Other. I'm making a web browser, so the app shouldn't be an Editor for anything, yet that's what I need to enable the New command.

I guess I need to override something, but where? The obvious candidates are the NSDocument subclass, a NSDocumentController subclass, or an application delegate. The first one is the only one I actually have so far (ie included by Xcode's default code), but it doesn't seem appropriate for NSDocument instances to create new ones. So I would have to create a NSDocumentController or NSApplicationDelgate subclass to do this, right?

So much of a newbie that I didn't completely RTFM.

The new-document command is handled by NSDocumentController . That class has a defaultType method that's used to determine which type a new document should be. By default it chooses the first type in the (static) list of document types that is set to "Editor" mode. It returns nil otherwise (ie Viewer and Utility apps).

A non-editing app can gain new documents by creating a subclass of NSDocumentController that at least overrides defaultType to return a non- nil string describing the document type for new documents. I used a MIME type, but I guess UTIs (and maybe extensions) also work.

But there's a new problem: your NSDocumentController won't be created by default; the default App setup ignores the class and makes a NSDocumentController object anyway. There's supposedly two ways around this: put an object of your subclass into the app's first NIB, the one with the main menu bar; or directly set the document-controller attribute to a new object of your subclass in one of the initialization methods of the application delegate. (Since the default document-based project templates don't include an application delegate class, you have to create that too.)

But I heard that either method is uncertain; if any previous part of application startup touches the NSDocument system, a default document-controller will be created and block out your object. Are there any Cocoa gurus out there that know where to put a custom NSDocumentController subclass object so it can't get pre-empted by a default document-controller?

...

I tried putting what I said in the third paragraph to work, but it doesn't. I keep getting " Date Time MyApp [ Number1 : Number2 ] The XXX type doesn't map to any NSDocumentClass." from the NSLog debug screen, where XXX is the string I return from defaultType . Tried both MIME types and UTIs. I'm stumped for the moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM