简体   繁体   English

NSDocument:如何保存和恢复文档的窗口位置?

[英]NSDocument: how do you save & restore a document's window position?

I have a NSDocument -based app, and I'd like my window positions to be saved and restored when re-opening documents. 我有一个基于NSDocument的应用程序,我希望在重新打开文档时保存和恢复我的窗口位置。 Apple's documentation on this is pretty sparse, but what I've been able to piece together is that at some point, something in the app needs to call NSWindow.setFrameUsingName() and NSWindow.setFrameAutosaveName() . Apple关于此的文档相当稀疏,但我能够拼凑起来的是,在某些时候,应用程序中的某些东西需要调用NSWindow.setFrameUsingName()NSWindow.setFrameAutosaveName()

What I haven't quite figured out is what point this needs to happen at, and what things need to do this. 我还没想到的是,这需要发生什么,以及需要做些什么。 For example, this doesn't work at all: 例如,这根本不起作用:

// In my NSDocument class    
override func windowControllerDidLoadNib(aController: NSWindowController) {
    super.windowControllerDidLoadNib(aController)

    // Add any code here that needs to be executed once the windowController has loaded the document's window.
    aController.window?.setFrameUsingName("MainWindow")
    aController.window?.setFrameAutosaveName("MainWindow")
}

I've read various different pieces of documentation or forum answers that point to awakeFromNib() to be another area to do this, but I can't get that to work either. 我已经阅读了各种不同的文档或论坛答案,指出awakeFromNib()是另一个要做的事情,但我也无法做到这一点。

I'm also confused / worried that this is somehow being affected by Auto Layout or something I've done wrong in Interface Builder - for example, this is how my window is set up in IB: 我也很困惑/担心这会在某种程度上受到Auto Layout的影响或者我在Interface Builder中做错了 - 例如,这就是我在IB中设置窗口的方式:

I don't particularly want my window centered, but the other options seem to lock it in place in fixed horizontal or fixed vertical positions, which I also don't really want. 我并不特别希望我的窗口居中,但其他选项似乎将它锁定在固定的水平或固定垂直位置,我也不是真的想要。 A side effect of having my window be centered is that my document windows no longer cascade, which I neither want nor can seem to stop from happening (note that windowController.shouldCascadeWindows = true isn't helping either). 让我的窗口居中的一个副作用是我的文档窗口不再级联,我既不想要也不能似乎停止发生(注意windowController.shouldCascadeWindows = true也没有帮助)。

So - what's going on here? 那么 - 这里发生了什么? I'm finding knowledge on this topic to be particularly unclear or misleading, and likely out of date for Cocoa development 2015, so a modern refresher on this would be great. 我发现关于这个主题的知识特别不清楚或误导,并且可能已经过时了2015年的可可开发,所以对此进行现代复习会很棒。

Step 1: In IB, give the window an autosave name. 步骤1:在IB中,为窗口指定自动保存名称。

在此输入图像描述

Step 2: There is no step 2. 第2步:没有第2步。

The easiest place to set the name of the autosave name for the window frame is probably in your implementation of NSDocument. 设置窗口框架自动保存名称的最简单位置可能在您的NSDocument实现中。

If you override makeWindowControllers() as part of the implementation, you are creating the window controller(s) manually, and thus can set the name there: 如果覆盖makeWindowControllers()作为实现的一部分,则手动创建窗口控制器,因此可以在其中设置名称:

override func makeWindowControllers() {
    let storyboard = NSStoryboard(name: NSStoryboard.Name("MyDocumentStoryboard"), bundle: nil)
    let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("MyDocument Window Controller")) as! MyDocumentWindowController

    // this assumes you have a stored property or a computed property `someUniqueIdentifier` that is as unique as possible for your document
    // and that you store on disk together with the rest of the document properties
    windowController.windowFrameAutosaveName = NSWindow.FrameAutosaveName(rawValue: someUniqueIdentifier)

    self.addWindowController(windowController)
}

If you instantiate your document window by overriding windowNibName , you should instead override the method windowControllerDidLoadNib(_:) , to set the autosave name on the window. 如果通过覆盖windowNibName实例化文档窗口,则应该覆盖方法windowControllerDidLoadNib(_:) ,以在窗口上设置自动保存名称。 I have not tested this code, but I assume it will work: 我没有测试过这段代码,但我认为它会起作用:

func windowControllerDidLoadNib(_ windowController: NSWindowController) {
    // this assumes you have a stored property or a computed property `someUniqueIdentifier` that is as unique as possible for your document
    // and that you store on disk together with the rest of the document properties
    windowController.windowFrameAutosaveName = NSWindow.FrameAutosaveName(rawValue: someUniqueIdentifier)
}

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

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