Ios6 UI status persistence and Restoration

Source: Internet
Author: User

The IOS design specification requires that when the application exits (including when it is terminated), the UI element status in the screen needs to be maintained, when you come in again, the status is the same as that when you exit. After ios6, Apple provided the following APIs to make it easy to maintain and restore the UI status.

In ios6, we can achieve State persistence and recovery in three places:

Application delegate object

View Controller

Custom View

To demonstrate the implementation of this function, we will transform the story board-based helloworld project and add a text box to the screen.

The user enters some content in the text box. When the application exits and comes in again after termination, the text box retains the original content. Select View Controller in Ib's scene, open the ID checker on the right, and set restoration ID to viewcontroller.

 

The recovery ID is the configuration item added by ios6 to achieve UI state persistence and restoration. We also need to make some modifications to the appdelegate code part of the application delegate object. The added code is as follows:

-(BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{return YES;} -(BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{return YES;} - (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder{[coder encodeFloat:2.0 forKey:@"Version"];} - (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder{float lastVer = [coder decodeFloatForKey:@"Version"];NSLog(@”lastVer = %f”,lastVer);}

 

 

Application: shouldsaveapplicationstate: called when the application exits. It controls whether the storage status is allowed. If yes is returned, it can be saved, and no is saved.

Application: shouldrestoreapplicationstate: it is called when the application is started. It is responsible for controlling whether to restore the status of the Last Exit. If yes is returned, it can be recovered. If no is returned, it cannot be recovered.

Application: willencoderestorablestatewithcoder: This method is called during storage. In this method, the UI status or data is saved. [coder encodefloat: 2.0 forkey: @ "version"] is used to save simple data.

Application: diddecoderestorablestatewithcoder: This method is called to restore the UI status or data in this method. The [coder decodefloatforkey: @ "version"] statement is used to restore the data stored last time.

To maintain and restore the control in a specific screen, you also need to add some code to its view controller. The code added to viewcontroller. m is as follows:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder{[super encodeRestorableStateWithCoder:coder];[coder encodeObject:self.txtField.text forKey:kSaveKey];} -(void)decodeRestorableStateWithCoder:(NSCoder *)coder{[super decodeRestorableStateWithCoder:coder];self.txtField.text = [coder decodeObjectForKey:kSaveKey];}

 

After ios6, two view controllers are added: encoderestorablestatewithcoder: And decoderestorablestatewithcoder: to save and restore controls or data in the controller. Here, encoderestorablestatewithcoder: The method is called during storage, [coder encodeobject: self.txt field. text forkey, saving and restoration are actually the process of encoding and decoding an archive file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.