【问题标题】:Under what conditions can an NSWindowController's document change?什么情况下 NSWindowController 的文档可以改变?
【发布时间】:2019-02-14 15:50:41
【问题描述】:

我的应用程序观察其NSWindowControllerdocument 属性,并在设置时执行一些UI 设置。设置后,由于更改而难以重建 UI(出于内部原因)。

一旦NSWindowController 将其document 属性设置为打开的文档,系统会在什么条件下更改该属性为新的NSDocument 实例(即,文档曾经被换掉)?我从未观察到这种情况发生,但我可以想象版本或 iCloud 同步等功能会导致窗口控制器的文档被交换为新文档。但是,NSWindowController 生命周期的文档似乎没有涉及到这个问题。

【问题讨论】:

  • 它只会改变一次,这是由 Document 方法保证的 - (void)makeWindowControllers 你调用 addWindowController:

标签: cocoa appkit nsdocument nswindowcontroller


【解决方案1】:

一旦设置就不会改变。 NSWindowController 通过addWindowController 方法获取它的文档。每个新的/打开的文档都会创建它自己的 windowController。文档的实例不会随 iCloud 或 revertChanges 改变。如何将文档与其视图同步(重绘)取决于您。

/* Create the user interface for this document, but don't show it yet. 
The default implementation of this method invokes [self windowNibName], 
creates a new window controller using the resulting nib name (if it is not nil), 
specifying this document as the nib file's owner, and then invokes [self addWindowController:theNewWindowController] to attach it. You can override 
this method to use a custom subclass of NSWindowController or to create more 
than one window controller right away. NSDocumentController invokes this method 
when creating or opening new documents.
*/


// e.g. override 
- (void)makeWindowControllers
{
    if ([[self windowControllers] count] == 0) {
        MainWindowController *controller = [[MainWindowController alloc] init];
        [self addWindowController:controller];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 2011-04-20
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多