使用应用关闭后的编码器快速保存ViewController状态

问题描述:

我正在使用Xcode 8和Swift 2.3

I am using Xcode 8 and swift 2.3

我想将整个视图控制器保存到文件并即使在应用关闭后也可以恢复状态. 我到处搜索,发现我们需要使用编码器.但是所有这些都只是显示要保存一个对象. 但是在这里,我需要保存整个ViewContoller和子视图.

I want to save the entire view controller to file and restore state even after app closes. I searched everywhere and found we need to use coder for that. but all just shows to save an object. but here I need to save entire ViewContoller and subviews.

ViewCotroller将具有三个按钮

ViewCotroller will have three buttons

  • 添加文本
  • 添加图像:用户可以添加任意数量的textView和图像.因此,我还需要保存所有这些信息.
  • 添加ViewController:用户可能具有此viewController的数组,需要保存所有.

问题1)

可以只保存self.view并自动保存所有子视图吗?

Can just save self.view and can it save all subviews automatically ?

问题2)

我只需要使用

let nameVccVar = nameVcc()

let nameVccVar = nameVcc(coder: CodeVar)

问题3)

如何使用NSKeyedUnarchiver将所有这些编码数据保存到文件中并取回?

How do I save all this coded data to file using NSKeyedUnarchiver and retrieve back?

请帮助我或给我一些提示,以完成所有工作

Kindly help me or give me tips to make all this work

class nameVcc: UIViewController
{
    var nameIntVar = 0
    var nameStringVar = "Save this"
    var nameImageVar = UIImage()

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    required init?(coder NkdPsgVar: NSCoder)
    {
        super.init(coder: NkdPsgVar)
    }

    override func encodeWithCoder(DkdPsgVar: NSCoder)
    {

    }

    func addTextViewBtnClick()
    {
        let viewVar = UIView()

        // Set many values for view 

        self.view.addSubview(viewVar)
    }

    func addImageViewBtnClick()
    {
        let imgViewVar = UIImageView()

        // Set many values for ImageView

        self.view.addSubview(imgViewVar)
    }
}

我也很累:

convenience init()
{
    self.init()
}

convenience init()
{
    self.init(coder: NSCoder())
}

TL; DR:
在您的应用中实现状态还原需要一些关键步骤和方法.

TL;DR:
There are key steps and methods required to implement state restoration in your app.

  1. 通过为这些方法shouldSaveApplicationStateshouldRestoreApplicationState返回true来加入App委托中的应用程序状态恢复.
  2. 为要在其中实现状态还原的视图控制器设置还原标识符.
  3. 在视图控制器中实现encodeRestorableStateWithCoderdecodeRestorableStateWithCoder方法.前者用于使用encodeObjectForKey方法将视图控制器的任何状态信息保存到磁盘.后者应用于通过decodeObjectForKey方法将状态从保存的内容恢复到磁盘上.
  1. Opting-in for App State Restoration in App Delegate by returning true for these methods shouldSaveApplicationState and shouldRestoreApplicationState.
  2. Setting Restoration Identifier for view controllers that you want state restoration implemented in.
  3. Implementing encodeRestorableStateWithCoder and decodeRestorableStateWithCoder methods in your view controller. The former is used to save any state information of your view controller to disk using encodeObjectForKey method. The latter shall be used to restore the state back from your saved contents to the disk by using decodeObjectForKey method.

观看有关[a href ="https://useyourloaf.com/blog/state-preservation-and-restoration/" rel ="nofollow noreferrer">状态恢复的精彩博客文章,以便更轻松地掌握.如果您有时间,请务必花时间观看状态恢复WWDC会话.

Watch this awesome blog post about State Restoration for easier grasp. If you have time, also do spend on watching State Restoration WWDC Session.

无需保存您自己的整个ViewController.设置还原标识符(在Interface Builder中)时,UIKit为您执行此操作.我们唯一需要集中精力进行状态恢复的就是保存重新创建应用程序的状态" 所需的必要属性,例如,一个Bool属性,确定您是否要显示特定按钮.

There is no need to save your entire ViewController yourself. UIKit does that for you when you set your Restoration Identifier (In Interface Builder). The only thing we need to focus for state restoration is to save your essential properties needed to re-create your app's "State", for example, a Bool property which determines whether you want to display a specific button or not.

现在来回答您的一系列问题....

我可以只保存self.view并自动保存所有子视图吗?

Can I just save self.view and can it save all subviews automatically ?

您不需要保存任何视图.所有子视图将由UIKit处理.编码所需的属性(例如 Bool 标志, count 变量,键属性),这些属性可用于从api调用中获取数据源的数据等.)在encodeRestorableStateWithCoder方法中.不要忘记从decodeRestorableStateWithCoder重新构造视图控制器的状态.这两种方法都属于UIStateRestoring协议.

You do not need to save any of your views. All subviews will be handled by UIKit. Encode your required properties (such as Bool flags, count variables, key properties that can be used to fetch data from api call for your datasource etc.) inside encodeRestorableStateWithCoder method. Don't forget to re-contruct your view controller's state from decodeRestorableStateWithCoder. Both of these methods belond to UIStateRestoring protocol.

我只需要使用

I need to init without coder at start by just using

无需执行任何幻想操作.

No need to do any fancy inits.

如何使用NSKeyedUnarchiver和 找回来?

How do I save all this coded data to file using NSKeyedUnarchiver and retrieve back?

正如我之前所说,实现必要的UIStateRestoring协议方法以保存和恢复应用程序的状态.

As I said earlier, implement necessary UIStateRestoring protocol methods to save and restore your app's state.