Encoding Relevant Data

To persist other information, a UIViewController is given a chance to encode any relevant data in a process very similar to archiving. In fact, both encoding processes use an NSCoder object to do the work. You will use this to save out any necessary information in the view controller subclasses.

In BNRDetailViewController.m, encode the itemKey for the currently displayed BNRItem.

-​ ​(​v​o​i​d​)​e​n​c​o​d​e​R​e​s​t​o​r​a​b​l​e​S​t​a​t​e​W​i​t​h​C​o​d​e​r​:​(​N​S​C​o​d​e​r​ ​*​)​c​o​d​e​r​
{​
 ​ ​ ​ ​[​c​o​d​e​r​ ​e​n​c​o​d​e​O​b​j​e​c​t​:​s​e​l​f​.​i​t​e​m​.​i​t​e​m​K​e​y​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​i​t​e​m​.​i​t​e​m​K​e​y​"​]​;​

 ​ ​ ​ ​[​s​u​p​e​r​ ​e​n​c​o​d​e​R​e​s​t​o​r​a​b​l​e​S​t​a​t​e​W​i​t​h​C​o​d​e​r​:​c​o​d​e​r​]​;​
}​

Then implement the decoding method to search through the BNRItemStore for the appropriate BNRItem.

-​ ​(​v​o​i​d​)​d​e​c​o​d​e​R​e​s​t​o​r​a​b​l​e​S​t​a​t​e​W​i​t​h​C​o​d​e​r​:​(​N​S​C​o​d​e​r​ ​*​)​c​o​d​e​r​
{​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​i​t​e​m​K​e​y​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​[​c​o​d​e​r​ ​d​e​c​o​d​e​O​b​j​e​c​t​F​o​r​K​e​y​:​@​"​i​t​e​m​.​i​t​e​m​K​e​y​"​]​;​

 ​ ​ ​ ​f​o​r​ ​(​B​N​R​I​t​e​m​ ​*​i​t​e​m​ ​i​n​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​a​l​l​I​t​e​m​s​]​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​[​i​t​e​m​K​e​y​ ​i​s​E​q​u​a​l​T​o​S​t​r​i​n​g​:​i​t​e​m​.​i​t​e​m​K​e​y​]​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​i​t​e​m​ ​=​ ​i​t​e​m​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​b​r​e​a​k​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​
 ​ ​ ​ ​}​

 ​ ​ ​ ​[​s​u​p​e​r​ ​d​e​c​o​d​e​R​e​s​t​o​r​a​b​l​e​S​t​a​t​e​W​i​t​h​C​o​d​e​r​:​c​o​d​e​r​]​;​
}​

Build and run the application and drill down into a BNRItem. Then perform the state restoration steps again. This time, the values on the BNRDetailViewController will correctly reflect the BNRItem being displayed.

There is still one problem: the text fields and labels are being populated with the values of the BNRItem. If the user has typed in some other value, those values will be lost upon state restoration.

You will fix this by saving the current text field values into the BNRItem. Since view controller encoding occurs after the application has entered the background, you will also need to save the store again.

In BNRDetailViewController.m update the encode method.

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

 ​ ​ ​ ​/​/​ ​H​a​v​e​ ​s​t​o​r​e​ ​s​a​v​e​ ​c​h​a​n​g​e​s​ ​t​o​ ​d​i​s​k​
 ​ ​ ​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​s​a​v​e​C​h​a​n​g​e​s​]​;​
 ​ ​ ​ ​ ​
 ​ ​ ​ ​[​s​u​p​e​r​ ​e​n​c​o​d​e​R​e​s​t​o​r​a​b​l​e​S​t​a​t​e​W​i​t​h​C​o​d​e​r​:​c​o​d​e​r​]​;​
}​

The BNRDetailViewController is now saving and restoring its state beautifully. Without a lot of work on your part, your users get a better experience. Now let’s turn our attention to the BNRItemsViewController.

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

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