5

我尝试在非基于文档的应用程序中在 Swift 中实现 NSWindowRestoration 协议。但是,该方法restoreWindowWithIdentifier永远不会在应用程序启动时调用。谁能指出我的错误?

这是代码的子集(编译和运行良好):

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowRestoration {

  var windowController : MyWindowController?

  func applicationDidFinishLaunching(aNotification: NSNotification?) {
    windowController = MyWindowController(windowNibName:"ImageSequenceView")
  }

  class func restoreWindowWithIdentifier(identifier: String!, state: NSCoder!, completionHandler: ((NSWindow!,NSError!) -> Void)!) {
    NSLog("restoreWindowWithIdentifier: \(identifier), state: \(state)")
  }

 }

class MyWindowController: NSWindowController {

  override func windowDidLoad() {
    super.windowDidLoad();
    window.restorationClass = AppDelegate.self
  }
}

提前致谢!

4

1 回答 1

3

您需要设置恢复类和标识符

class MyWindowController: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()

        self.window?.restorationClass = type(of: self)
        self.window?.identifier = "MyWindow"
    }
}

extension MyWindowController: NSWindowRestoration {
    static func restoreWindow(withIdentifier identifier: String, state: NSCoder, completionHandler: @escaping (NSWindow?, Error?) -> Void) {
        if identifier == "MyWindow" {
            // Restore the window here
        }
    }
}

当然你也可以让另一个类恢复窗口,就像你试过的那样。在这种情况下,您需要分配AppDelegate.selfrestorationClass

另外,请注意,无论出于何种愚蠢的原因,窗口恢复设置现在默认为 "off" 。

于 2017-06-28T09:03:56.587 回答