justin k.
justin k.

Reputation: 504

How to automatically create new NSDocument if no documents are restored?

EDIT: It seems like the issue below only occurs (rather, fails to occur... :) when I run the from within Xcode (I'm on 4.4), rather than from Finder. Is this an Xcode bug, or am I missing something?

I want to create a new (untitled) NSDocument when the app starts up, in the case that no documents were automatically restored. Both TextEdit and Safari display the behavior I want, so it seems like this should be built-in, but I can't find any option for it.

Steps for recreating my problem: if I create a new document-based app in Xcode, and run it, the first time it runs it indeed creates an untitled document. But, if I close that document, quit the app, and run it again, it doesn't give me a new document, unless I explicitly click on the app icon in the Dock. This is different than e.g. TextEdit, where if you quit and relaunch, you get a new empty document. (Changing the bundle identifier gives me another new untitled document, as expected.)

My first thought was that maybe this should occur in applicationDidFinishLaunching:, but it turns out that that method is executed before any previously opened documents are restored.

Upvotes: 3

Views: 2112

Answers (1)

Stephan
Stephan

Reputation: 4263

This behaviour is part of the document architecture. Your application delegate should implement the following method of the Protocol NSApplicationDelegate

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
{
    return YES;
}

You find more about this here.

Upvotes: 4

Related Questions