2017-05-04 225 views
0

任何人都可以指向描述MacOS上状态保存和恢复的资源。我一直无法找到任何详细的非IOS资源(指南,示例代码等)。我确实在核心应用程序设计https://developer.apple.com/library/content/documentation/General/Conceptual/MOSXAppProgrammingGuide/CoreAppDesign/CoreAppDesign.html#//apple_ref/doc/uid/TP40010543-CH3-SW26以及此视频https://developer.apple.com/videos/play/wwdc2016/239/?time=2273中找到了一些段落,但它们没有提供完整的图片。我正在使用Swift 3,XCode 8.2.1和Macos 10.11.6在MacOs上保存和恢复状态

任何帮助非常感谢。

回答

0
+0

Thx为回应。我找到了文档。大书呆子牧场的例子在Objective C中,我没有足够的熟悉翻译Swift。我认为,如果我可以计算出'restoreWindow(withIdentifier:String,state NSCoder,completionHandler:@escaping(NSWindow ?, Error?) - > Void) - > Void'class function需要什么,我会忍受一次战斗机会获得它的工作。 - 另外,我发现令人惊讶和沮丧的是,MacOS上基于Swift的资源非常少。 –

+0

@LesOrmonde https://github.com/kfix/MacPin/blob/a81d77438b57932466c7461700b0d4629ea74d/modules/MacPin/OSX/AppDelegateOSX.swift#L373怎么样? –

0

更新: 我已经取得了一些进步的主要窗口保存和恢复。在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。谁能帮忙?