繁体   English   中英

基于文档的应用程序的“新建文档”停靠菜单项不会打开新文档

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

我的基于文档的应用程序有一个带有“新文档”项的停靠菜单。 停靠菜单是在Interface Builder中创建的,其项的动作已连接到“第一响应者”的-newDocument:

文档控制器是NSDocumentController的子类,称为DocumentController

在应用程序委托中,此代码用于防止启动时打开无标题的文档(而是显示文档控制器的打开面板):

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

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

}

如果我现在启动我的应用程序,它将显示打开的面板而不是未命名的文档。 如果然后单击停靠菜单的“新建文档”项,则不会打开任何新文档。 如果我在模板主菜单中单击标准文件菜单选项“新建文档”,则会打开一个新文档。

我不知道为什么会这样,是吗? 如何获得停靠菜单以打开新文档?


编辑: 这是一个示例项目,它没有NSDocumentController子类,但仍然存在相同的问题。

菜单项在文档控制器上调用newDocument方法。 如果单击扩展坞,则会触发NSApplication东西及其委托。 例如,如果您有一个现有的应用程序窗口,则不会在那里被调用。 没有窗口会触发applicationShouldOpenUntitledFile

在您的文档控制器中重写如下:

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

如果要在启动后显示openPanel,请不要响应appdelegate(应为bla bla)。 如果要在用户单击图标时触发它,请执行第二种方法

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

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

基于文档的应用程序中“文件”菜单的“新建”项的操作。 此方法的默认实现调用-openUntitledDocumentAndDisplay:error :,如果返回nil,则在应用程序模式面板中显示错误。

  • (IBAction为)新建文档:(ID)发送者;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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