简体   繁体   中英

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

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.

Menu item calls newDocument method on the document controller. If you click on the dock you trigger NSApplication stuff and it's delegate. Eg 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;

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