使用 NSDocumentController 子类创建新文档

Creating a new document with an NSDocumentController subclass

我有一个 NSDocumentController 子类,我正在覆盖 -newDocument: 方法。

如何创建带有标题的新文档?这是我所用的简短版本,但文档首先显示为 "Untitled",然后更改为我的自定义标题。我希望使用自定义标题立即打开新文档。

MyDocument *document = (MyDocument *)[self openUntitledDocumentAndDisplay:YES error:&error];
[document setDisplayName:@"My Title"];

我试过了,但对我不起作用。文档没有显示。

MyDocument *document = (MyDocument *)[self openUntitledDocumentAndDisplay:NO error:&error];
[document setDisplayName:@"My Title"];
[document showWindows]; 

您错过了使 window 控制器与文档关联的步骤。

[document showWindows]; 仅显示已与文档关联的 windows。

需要发文件-makeWindowControllers,或者自己制作,酌情发文件-addWindowController:再发-showWindows.