Fra*_*ert 5 cocoa state-restoration swift
我尝试在 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
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
您需要设置恢复类别和标识符:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
当然,您也可以让另一个类恢复窗口,就像您尝试过的那样。在这种情况下,您需要指定AppDelegate.self为restorationClass。
另外,请注意,出于任何愚蠢的原因,窗口恢复设置现在默认为“off”。
| 归档时间: |
|
| 查看次数: |
884 次 |
| 最近记录: |