我们希望在输入一些字符,当App关掉再次运行的时候之前输入的那些字符会自动显示在Text中.
然后在AppDelegate.m中添加如下回调方法:
-(BOOL)application:(UIApplication*)application shouldSaveApplicationState: (nonnull NSCoder *)coder{returnYES;}-(BOOL)application:(UIApplication*)application shouldRestoreApplicationState:(NSCoder *)coder{returnYES;}
如果你想要保存App级别的数据可以使用以下2个回调方法:
-(void)application:(UIApplication*)application didDecodeRestorableStateWithCoder:(NSCoder*)coder{
float val = [coder decodeFloatForKey:kAppValue];
NSLog(@"value is %f",val);
}
-(void)application:(UIApplication*)application willEncodeRestorableStateWithCoder:(nonnullNSCoder*)coder{
[coder encodeFloat:99.99forKey:kAppValue];
}
如果你想要保存某个View界面中的数据,就像一开始说的那样,你可以在ViewController.m里添加如下2个方法:
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder{
[superdecodeRestorableStateWithCoder:coder];
self.inputText.text= [coder decodeObjectForKey:kSaveKey];
}
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder{
[superencodeRestorableStateWithCoder:coder];
[coder encodeObject:self.inputText.textforKey:kSaveKey];
}
现在你运行App,输入一些字符然后关闭App.当你再次运行该App时你会发现View界面中的文本框会显示之前输入的字符串: