Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift NSDocument:如何保存&;还原文档';窗户的位置?_Swift_Cocoa_Osx Yosemite_Nsdocument - Fatal编程技术网

Swift NSDocument:如何保存&;还原文档';窗户的位置?

Swift NSDocument:如何保存&;还原文档';窗户的位置?,swift,cocoa,osx-yosemite,nsdocument,Swift,Cocoa,Osx Yosemite,Nsdocument,我有一个基于NSDocument的应用程序,我希望在重新打开文档时保存和恢复我的窗口位置。苹果关于这方面的文档非常稀少,但我能够拼凑起来的是,在某个时候,应用程序中的某些东西需要调用NSWindow.setFrameUsingName()和NSWindow.setFrameAutosaveName() 我还没有弄清楚这需要发生在什么时候,需要做什么。例如,这根本不起作用: // In my NSDocument class override func windowControllerDi

我有一个基于
NSDocument
的应用程序,我希望在重新打开文档时保存和恢复我的窗口位置。苹果关于这方面的文档非常稀少,但我能够拼凑起来的是,在某个时候,应用程序中的某些东西需要调用
NSWindow.setFrameUsingName()
NSWindow.setFrameAutosaveName()

我还没有弄清楚这需要发生在什么时候,需要做什么。例如,这根本不起作用:

// 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")
}
我已经阅读了各种不同的文档或论坛答案,它们指出
awakeFromNib()
是实现这一点的另一个领域,但我也无法实现这一点

我还感到困惑/担心的是,自动布局或我在Interface Builder中所做的一些错误会影响到这一点-例如,我的窗口是如何在IB中设置的:

我并不特别希望我的窗口居中,但其他选项似乎将其锁定在固定水平或固定垂直位置,我也不希望这样。将窗口居中的一个副作用是,我的文档窗口不再层叠,我既不希望也似乎无法阻止这种情况的发生(请注意,
windowController.shouldCascadeWindows=true


那么-这是怎么回事?我发现关于这个主题的知识特别不清楚或有误导性,对于Cocoa development 2015来说可能已经过时了,因此关于这个主题的现代复习将是非常好的。

步骤1:在IB中,给窗口一个autosave名称


步骤2:没有步骤2。

为窗口框架设置自动保存名称的最简单位置可能是在NSDocument的实现中

如果作为实现的一部分重写
MakeWindowController()
,则您是在手动创建窗口控制器,因此可以在其中设置名称:

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)
}
如果您通过覆盖
windowNibName
来实例化文档窗口,则应替代方法
windowcontrolleridloadnib(:)
,以在窗口上设置自动保存名称。我还没有测试过这段代码,但我认为它可以工作:

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)
}

我发现这有两个问题:它使每个NSDocument窗口使用相同的原点,并且它不恢复窗口的大小,只恢复原点。我将框架存储在文档数据中,但它总是在默认位置打开文档窗口,然后移动它,这看起来很糟糕。必须有一个标准的解决方案。与@SarahR相同的问题