标签:
数据保存于恢复
用户操作(输入数据)之后,应用程序退出并且终止之后,当用户再次打开应用的时候还是保持原来的状态
一:在storyBoard中设置恢复标志符
二:在AppDalegate中代理方法
1 -(BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder 2 { 3 return YES; 4 } 5 6 -(BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder 7 { 8 return YES; 9 } 10 11 - (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder 12 { 13 [coder encodeFloat:2.0 forKey:@"Version"]; 14 } 15 16 - (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder 17 { 18 float lastVer = [coder decodeFloatForKey:@"Version"]; 19 NSLog(@"lastVer = %f",lastVer); 20 }
三:在ViewController中实现归档相应的方法:
1 -(void)encodeRestorableStateWithCoder:(NSCoder *)coder 2 { 3 [super encodeRestorableStateWithCoder:coder]; 4 [coder encodeObject:self.txtField.text forKey:kSaveKey]; 5 } 6 7 -(void)decodeRestorableStateWithCoder:(NSCoder *)coder 8 { 9 [super decodeRestorableStateWithCoder:coder]; 10 self.txtField.text = [coder decodeObjectForKey:kSaveKey]; 11 }
注:这里使用的原理就是归档,如果是一些其他数据的话直接使用归档就可以,这里因为是storyBoard所以使用到了恢复标志符
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4555722.html