通过Swift在Cocoa中实现UI保存/恢复机制(UI Save/Restoration mechanism in Cocoa via Swift)

系统教程 行业动态 更新时间:2024-06-14 16:57:17
通过Swift在Cocoa中实现UI保存/恢复机制(UI Save/Restoration mechanism in Cocoa via Swift)

我想保存Check Box的状态,退出应用程序,然后再次启动macOS应用程序以查看Check Box恢复状态。 但是我的应用的用户界面中没有恢复状态。

我究竟做错了什么?

import Cocoa class ViewController: NSViewController { @IBOutlet weak var tick: NSButton! override func viewDidLoad() { super.viewDidLoad() } override func encodeRestorableState(with coder: NSCoder) { super.encodeRestorableState(with: coder) coder.encode(tick.state, forKey: "") } override func restoreState(with coder: NSCoder) { super.restoreState(with: coder) if let state = coder.decodeObject(forKey: "") as? NSControl.StateValue { tick.state = state } } }

I'd like to save the state of Check Box, quit application, then launch macOS app again to see restored state of my Check Box. But there's no restored state in UI of my app.

What am I doing wrong?

import Cocoa class ViewController: NSViewController { @IBOutlet weak var tick: NSButton! override func viewDidLoad() { super.viewDidLoad() } override func encodeRestorableState(with coder: NSCoder) { super.encodeRestorableState(with: coder) coder.encode(tick.state, forKey: "") } override func restoreState(with coder: NSCoder) { super.restoreState(with: coder) if let state = coder.decodeObject(forKey: "") as? NSControl.StateValue { tick.state = state } } }

最满意答案

就我所知,这是实现窗口和/或其内容的自定义用户界面状态恢复所需的绝对最低限度。

在这个例子中,我有一个带复选框的窗口,该复选框的状态代表了一些自定义视图状态,当应用程序重新启动时,我想要恢复它。

该项目包含一个带有单个复选框按钮的窗口。 该按钮的值绑定到窗口的内容视图控制器的myState属性。 所以,在技术上,这是一个复选框控件的事实是不相关的; 我们实际上是要保存和恢复myState属性(UI自我照顾)。

为了做到这一点,窗口的restorable属性设置为true (在窗口对象检查器中),窗口被分配一个标识符( "PersistentWindow" )。 NSWindow是子类( PersistentWindow ),子类实现restorableStateKeyPaths属性。 该属性列出了要保存/恢复的自定义属性。

注意:如果您可以按照键值兼容属性路径的列表定义您的UI状态还原,那是(迄今为止)最简单的解决方案。 如果不是,则必须实现encodeRestorableState / restoreState并负责调用invalidateRestorableState 。

这是自定义窗口类:

class PersistentWindow: NSWindow { // Custom subclass of window the perserves/restores UI state // The simple way to preserve and restore state information is to just declare the key-value paths // of the properties you want preserved/restored; Cocoa does the rest override class var restorableStateKeyPaths: [String] { return [ "self.contentViewController.myState" ] } // Alternatively, if you have complex UI state, you can implement these methods // override func encodeRestorableState(with coder: NSCoder) { // // optional method to encode special/complex view state here // } // // override func restoreState(with coder: NSCoder) { // // companion method to decode special/complex view state // } }

这里是内容视图控制器的(相关部分)

class ViewController: NSViewController { @objc var myState : Bool = false blah, blah, blah }

(我将它构建为一个Cocoa应用程序项目,如果有人告诉我可以上传它,我可以上传它。)

To the best of my knowledge, this is the absolute minimum you need to implement custom UI state restoration of a window and/or its contents.

In this example, I have a window with a checkbox and that checkbox's state represents some custom view state that I want to restore when the app is relaunched.

The project contains a single window with a single checkbox button. The button's value is bound to the myState property of the window's content view controller. So, technically, the fact that this is a checkbox control is irrelevant; we're actually going to preserve and restore the myState property (the UI takes care of itself).

To make this work, the window's restorable property is set to true (in the window object inspector) and the window is assigned an identifier ("PersistentWindow"). NSWindow is subclassed (PersistentWindow) and the subclass implements the restorableStateKeyPaths property. This property lists the custom properties to be preserved/restored.

Note: if you can define your UI state restoration in terms of a list of key-value compliant property paths, that is (by far) the simplest solution. If not, you must implement encodeRestorableState / restoreState and are responsible for calling invalidateRestorableState.

Here's the custom window class:

class PersistentWindow: NSWindow { // Custom subclass of window the perserves/restores UI state // The simple way to preserve and restore state information is to just declare the key-value paths // of the properties you want preserved/restored; Cocoa does the rest override class var restorableStateKeyPaths: [String] { return [ "self.contentViewController.myState" ] } // Alternatively, if you have complex UI state, you can implement these methods // override func encodeRestorableState(with coder: NSCoder) { // // optional method to encode special/complex view state here // } // // override func restoreState(with coder: NSCoder) { // // companion method to decode special/complex view state // } }

And here's the (relevant portion) of the content view controller

class ViewController: NSViewController { @objc var myState : Bool = false blah, blah, blah }

(I built this as a Cocoa app project, which I could upload if someone tells me where I could upload it to.)

更多推荐

本文发布于:2023-04-12 20:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/85b561612096a775b077de84cea290ad.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机制   UI   Cocoa   Swift   Restoration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!