更新: 我已经取得了一些进步的主要窗口保存和恢复。在MainWindowController我已经加入
window?.isRestorable = true
window?.restorationClass = AppDelegate.self
和
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
log.pp(tag: logTag, content: #function + "Encoding")
coder.encode(testValue, forKey: "testValue")
}
override func restoreState(with coder: NSCoder) {
super.restoreState(with: coder)
testValue = (coder.decodeObject(forKey: "testValue") as? String)!
log.pp(tag: logTag, content: #function + "value of restored testValue is \(testValue)")
}
在AppDelegate中我有
class func restoreWindow(withIdentifier: String,
state: NSCoder,
completionHandler: @escaping(NSWindow?, Error?) -> Void) -> Void {
print("\n" + #function + " identifier: \(withIdentifier), state: \(state)")
if let mainWindow = NSApplication.shared().mainWindow {
completionHandler(mainWindow, nil)
print("\n" + #function + "completionhandler reinstated main window: \(mainWindow) identifier \(withIdentifier)")
}
else {
completionHandler(nil, nil)
print("\n" + #function + "completionhandler failed to reinstate main window error")
}
}
的主视图控制器包含
// Saving and Restoration of State
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
log.pp(tag: idTag, content: #function + "Encoding state")
coder.encode(testValue, forKey: "MVCStateTest")
}
override func restoreState(with coder: NSCoder) {
super.restoreState(with: coder)
if let string = coder.decodeObject(forKey: "MVCStateTest") as? String! {
log.pp(tag: idTag, content: #function + "value of restored testValue is \(testValue)")
testValue = string
}
}
这一切都似乎工作除了对应的视图没有恢复(即可见)。
Mac的应用程序向导说,
可可使用返回的窗口进行恢复和保存任何响应者对象到之前的状态。 - StandardCocoa窗口和视图对象恢复到以前的状态,没有额外的帮助。如果您继承NSWIndow或NSView,请实施restoreState WIthCoder:方法以恢复任何自定义状态。
这就是没有发生。主视图控制器包含一组由用户单击工具栏项目添加的tabViewController。这些TabViewController具有必需的编码和恢复方法,但不会被调用。
已经在互联网上寻找指导,但是这样的帮助存在于所有基于IOS的平台上,并且不能直接转换成MacOS。谁能帮忙?
Thx为回应。我找到了文档。大书呆子牧场的例子在Objective C中,我没有足够的熟悉翻译Swift。我认为,如果我可以计算出'restoreWindow(withIdentifier:String,state NSCoder,completionHandler:@escaping(NSWindow ?, Error?) - > Void) - > Void'class function需要什么,我会忍受一次战斗机会获得它的工作。 - 另外,我发现令人惊讶和沮丧的是,MacOS上基于Swift的资源非常少。 –
@LesOrmonde https://github.com/kfix/MacPin/blob/a81d77438b57932466c7461700b0d4629ea74d/modules/MacPin/OSX/AppDelegateOSX.swift#L373怎么样? –