For the More Curious: State Restoration

We discussed how to work with the state restoration system in Chapter 24, but the Homepwner application did not use storyboards. Let’s take a look at how state restoration works when using storyboards.

Storyboards handle a lot of the boilerplate state restoration code for you. Within a storyboard file, each view controller’s restoration identifier can be set. Typically, the restoration identifier is set to be the same as the storyboard identifier (which you did not need to use in this chapter). For state restoration to work properly, you will want each view controller to have both a storyboard identifier and a restoration identifier (Figure 28.22).

Figure 28.22  Restoration identifier

Restoration identifier

Since state restoration is opt-in, you will still need to override the two application delegate methods to tell the system you want states to be saved and restored.

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
s​h​o​u​l​d​S​a​v​e​A​p​p​l​i​c​a​t​i​o​n​S​t​a​t​e​:​(​N​S​C​o​d​e​r​ ​*​)​c​o​d​e​r​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
s​h​o​u​l​d​R​e​s​t​o​r​e​A​p​p​l​i​c​a​t​i​o​n​S​t​a​t​e​:​(​N​S​C​o​d​e​r​ ​*​)​c​o​d​e​r​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

Finally, your UIViewController subclasses will each need to implement the UIViewControllerRestoration protocol method to return an instance of the appropriate view controller. Since you are working with storyboards, you will let the storyboard instantiate the view controller for you. Here is an example:

+​ ​(​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​)​v​i​e​w​C​o​n​t​r​o​l​l​e​r​W​i​t​h​R​e​s​t​o​r​a​t​i​o​n​I​d​e​n​t​i​f​i​e​r​P​a​t​h​:​(​N​S​A​r​r​a​y​ ​*​)​p​a​t​h​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​c​o​d​e​r​:​(​N​S​C​o​d​e​r​ ​*​)​c​o​d​e​r​
{​
 ​ ​ ​ ​B​N​R​C​o​l​o​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​v​c​ ​=​ ​n​i​l​;​

 ​ ​ ​ ​U​I​S​t​o​r​y​b​o​a​r​d​ ​*​s​t​o​r​y​b​o​a​r​d​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​[​c​o​d​e​r​ ​d​e​c​o​d​e​O​b​j​e​c​t​F​o​r​K​e​y​:​U​I​S​t​a​t​e​R​e​s​t​o​r​a​t​i​o​n​V​i​e​w​C​o​n​t​r​o​l​l​e​r​S​t​o​r​y​b​o​a​r​d​K​e​y​]​;​
 ​ ​ ​ ​i​f​ ​(​s​t​o​r​y​b​o​a​r​d​)​
 ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​v​c​ ​=​ ​(​B​N​R​C​o​l​o​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​)​[​s​t​o​r​y​b​o​a​r​d​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​n​s​t​a​n​t​i​a​t​e​V​i​e​w​C​o​n​t​r​o​l​l​e​r​W​i​t​h​I​d​e​n​t​i​f​i​e​r​:​@​"​B​N​R​C​o​l​o​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​v​c​.​r​e​s​t​o​r​a​t​i​o​n​I​d​e​n​t​i​f​i​e​r​ ​=​ ​[​i​d​e​n​t​i​f​i​e​r​C​o​m​p​o​n​e​n​t​s​ ​l​a​s​t​O​b​j​e​c​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​v​c​.​r​e​s​t​o​r​a​t​i​o​n​C​l​a​s​s​ ​=​ ​[​B​N​R​C​o​l​o​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​c​l​a​s​s​]​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​v​c​;​
}​

The NSCoder automatically encodes a reference to the storyboard that you can pull out using the UIStateRestorationViewControllerStoryboardKey key. You can then use the storyboard to instantiate the appropriate view controller, passing in the correct storyboard identifier.

Other than that, the rest of state restoration implementation is the same. If view controllers need to save out any information, then they will need to implement the encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: methods.

Whether you are using storyboards or not, state restoration is easy to implement and provides a better experience for your users. Be a stylish developer; implement state restoration in your applications today!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset