gpt4 book ai didi

macos - NSViewControllers和NSViews没有获得恢复API调用

转载 作者:行者123 更新时间:2023-12-04 12:57:23 26 4
gpt4 key购买 nike

在Apple文档中,他们说,在基于文档的应用程序中,您应该获得对NSApplication,NSWindow,NSWindowController以及整个响应者链(here)的恢复调用(restoreStateWithCoder:和encodeRestorableStateWithCoder :)。

我想实现这一点,但是我只能在NSWindowController/NSDocument子类中获得恢复调用,而不能在NSViews或NSViewControllers中得到恢复调用。

我创建了一个新的基于文档的应用程序对此进行测试(here),但是我仅在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
}
}

最佳答案

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

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

关于macos - NSViewControllers和NSViews没有获得恢复API调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42010026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com