NSViewControllers和NSView没有获得恢复API调用

Pav*_*kal 7 macos state-restoration

在Apple的文档中,他们说如果是基于文档的应用程序,你应该恢复调用(restoreStateWithCoder:和encodeRestorableStateWithCoder :)到NSApplication,NSWindow,NSWindowController,然后是整个响应者链(这里).

我想实现这个,但我只在NSWindowController/NSDocument子类中获得恢复调用,而不是NSViews或NSViewControllers.

我创建了一个新的基于文档的应用程序来测试这个(这里),但我只在NSDocument子类中获得了恢复调用,但是没有NSViewController或NSView.

测试项目的代码:

NSDocument子类(恢复工作):

class Document: NSDocument {

  override func restoreState(with coder: NSCoder) {
    super.restoreState(with: coder)
    // Gets called after reopening the app
  }

  override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    // Gets called when leaving the window for the first time
  }


}
Run Code Online (Sandbox Code Playgroud)

NSViewController子类(恢复不起作用):

class ViewController: NSViewController {

  override func restoreState(with coder: NSCoder) {
    super.restoreState(with: coder)
    // Not called
  }

  override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    // Not called
  }

}
Run Code Online (Sandbox Code Playgroud)

NSView子类(恢复不起作用):

class MyView: NSView {

  override func restoreState(with coder: NSCoder) {
    super.restoreState(with: coder)
    // Not called
  }

  override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    // Not called
  }
}
Run Code Online (Sandbox Code Playgroud)

fum*_*007 3

我刚刚遇到了同样的问题。状态保存系统似乎使用该identifier属性来确定是否编码/恢复对象的状态。

根据文档,当从 NIB 文件加载对象时,标识符会自动填充。但是,如果以编程方式创建对象,则需要手动设置。