IOS 6 UI status maintenance 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: the application delegates the custom view of the object View Controller. To demonstrate the implementation of this function, we transform the HelloWorld project based on the story board, add a text box to the screen and enter 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 modify the AppDelegate code part of the application delegate object. The added code is as follows: [cpp]-(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 forK Ey: @ "Version"];}-(void) application :( UIApplication *) application didDecodeRestorableStateWithCoder :( NSCoder *) coder {float lastVer = [coder decodeFloatForKey: @ "Version"]; NSLog (@ "lastVer = % f", lastVer);}-(BOOL) application :( UIApplication *) application shouldSaveApplicationState :( NSCoder *) coder {return YES;}-(BOOL) application :( UIApplication *) application shouldRestoreApplicationState :( NSC Oder *) coder {return YES;}-(void) application :( UIApplication *) application willEncodeRestorableStateWithCoder :( NSCoder *) coderwww.2cto.com {[coder encodeFloat: 2.0 forKey: @ "Version"]; -(void) application :( UIApplication *) application didDecodeRestorableStateWithCoder :( NSCoder *) coder {float lastVer = [coder decodeFloatForKey: @ "Version"]; NSLog (@ "lastVer = % f ", lastVer);} Where application: shouldSa VeApplicationState: called when the application exits. It is responsible for controlling 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 in its view controller, ViewController. the code added in m is as follows: [cpp]-(void) encodeRestorableStateWithCoder :( NSCoder *) coder {[super encodeRestorableStateWithCoder: coder]; [coder encodeObject: self.txt Field. text forKey: kSaveKey];}-(void) decodeRestorableStateWithCoder :( NSCoder *) coder {[super decodeRestorableStateWithCoder: coder]; self.txt Field. text = [coder decodeObjectForKey: kSaveKey];}-(void) EncodeRestorableStateWithCoder :( NSCoder *) coder {[super encodeRestorableStateWithCoder: coder]; [coder encodeObject: self.txt Field. text forKey: kSaveKey];}-(void) decodeRestorableStateWithCoder :( NSCoder *) coder {[super decodeRestorableStateWithCoder: coder]; self.txt Field. text = [coder decodeObjectForKey: kSaveKey];} added two view controllers after iOS6: encodeRestorableStateWithCoder: And decodeRestorableStateWithCoder: It is used 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.

Related Article

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.