关闭文档窗口并打开一个新窗口,而不关闭文档

时间:2009-12-18 11:33:33

标签: cocoa nsdocument appkit nswindowcontroller

我的应用是基于文档的,但“文档”包含两个文件夹,而不是一个文件。文档的初始窗口包含几个文件选择器和一个按钮;该操作将关闭该窗口并打开一个新窗口,显示两个文件夹层次结构之间的操作结果。 (这两个窗口的大小差别很大;将两个视图保存在tabless tabview中并使用它进行切换将是非常重要的。)

以下是我的操作方法中关闭文件拣取程序窗口并打开结果窗口的代码:

[self retain];
NSArray *existingWindowControllers = [[[self windowControllers] copy] autorelease];
for (NSWindowController *windowController in existingWindowControllers) {
    [windowController setShouldCloseDocument:NO];
    [windowController close];
    [self removeWindowController:windowController];
}
[self addWindowController:[[[NSWindowController alloc] initWithWindowNibName:@"ProjectFoldersDocument" owner:self] autorelease]];
[self showWindows];
[self release];

(我在尝试解决问题时添加了保留和释放消息。)

我的问题是,在此操作方法完成后,文档将被释放并取消分配,尽管我告诉初始窗口控制器不要关闭文档。 (这是解决问题的另一次尝试失败。)

那么,如何在没有文档消失的情况下将同一文档的第一个窗口替换为另一个窗口?

1 个答案:

答案 0 :(得分:1)

我终于通过切换removeWindowController:close消息解决了这个问题:

[self removeWindowController:windowController];
[windowController close];

这表明窗口控制器正在关闭时关闭其文档。我不知道为什么,因为我告诉它不要紧接在前一行。

我还删除了明确的retainrelease消息。问题没有回来。