在 Apple 的文档中,他们说在基于文档的应用程序的情况下,您应该对 NSApplication、NSWindow、NSWindowController 以及整个响应程序链(此处)进行恢复调用(restoreStateWithCoder: 和 encodeRestorableStateWithCoder:)。
我想实现这一点,但我只在 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
}
}
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
}
}
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
}
}