Swift Save viewcontroller state using coder beyond app close

早过忘川 提交于 2020-01-30 06:33:25

问题


I am using Xcode 8 and swift 2.3

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 will have three buttons

  • Add Text
  • Add Image : User can add any number of textViews and Images. So I need to save all that info also.
  • Add ViewController : User may have an array of this viewController and need to save all.

Question 1)

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

Question 2)

I need to init without coder at start by just using

let nameVccVar = nameVcc()

and

let nameVccVar = nameVcc(coder: CodeVar)

Question 3)

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)
    }
}

I also tired :

convenience init()
{
    self.init()
}

and

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

回答1:


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

  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.

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


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.

Now coming to your series of questions....

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

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.

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

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



来源:https://stackoverflow.com/questions/46394378/swift-save-viewcontroller-state-using-coder-beyond-app-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!