Document-based app's 'New Document' dock menu item won't open new document

559 views Asked by At

My document-based app has a dock menu with a 'New Document' item. The dock menu is made in Interface Builder, its item's action is connected to 'First Responder's -newDocument:

The document controller is a subclass of NSDocumentController called DocumentController.

In the app delegate this code is used to prevent the opening of an untitled document upon launch (instead the document controller's open panel is shown) :

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {

    [(DocumentController *)[NSDocumentController sharedDocumentController] openDocument:self];
    return NO;

}

If I now launch my app, it'll show the open panel instead of an untitled document. If I then click on the dock menu's 'New Document' item, no new document opens. If I click on the standard file menu option 'New Document' in the template main menu, a new document does open.

I can't think of a reason as to why this is so, can you? How do I get the dock menu to open a new document?


EDIT : Here is a sample project which has no NSDocumentController subclass but still has the same problem.

1

There are 1 answers

7
Marek H On

Menu item calls newDocument method on the document controller. If you click on the dock you trigger NSApplication stuff and it's delegate. E.g. if you have a existing app window than you will not get called there. No window will trigger applicationShouldOpenUntitledFile

In your document controller override like this:

- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error:(NSError *__autoreleasing *)outError {
  [self openDocument:nil]
}

And instead of responding to appdelegate(should bla bla) do this if you want after launch to display openPanel. If you want to trigger it when user clicks on the icon then do the second method

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [(DocumentController *)[NSDocumentController sharedDocumentController] openDocument:nil];
}

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

The action of the File menu's New item in a document-based application. The default implementation of this method invokes -openUntitledDocumentAndDisplay:error: and, if nil is returned, presents the error in an application-modal panel.

  • (IBAction)newDocument:(id)sender;