我目前很难理解应该使用什么来保存和恢复我的应用状态。
我正在使用一个情节提要,并且有很多ViewController,并且我想在应用终止时保存所有导航堆栈,以便能够在用户重新启动应用时恢复所有导航。
我正在将其UINavigationController
与另一个UINavigationController一起使用,仅供参考。
我找到了这个并且读了一遍遍:https : //developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html#//apple_ref/doc/uid/TP40007457-CH28-SW34
(必需)为要保留其配置的视图控制器分配恢复标识符;请参阅标记View Controllers for Preservation。
(必需)告诉iOS如何在启动时创建或定位新的视图控制器对象;请参阅在启动时还原View Controller。
然后我RestorationId
在所有ViewController上添加了一个,但是我不明白第二部分应该做什么,因为当我添加viewControllerWithRestorationIdentifierPath
未传递给内部的方法时。
我还尝试将它们保存navigation.viewcontrollers
到NSUserDefaults中,以便在用户使用以下代码重新启动应用程序时再次使用它们:
+(NSArray *) getNavStatus
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
id objectSaved;
if( (objectSaved = [preferences objectForKey:navStatusKey]) != nil)
return [NSKeyedUnarchiver unarchiveObjectWithData:objectSaved];
else
return nil;
}
+(BOOL) saveNavStatus:(UINavigationController *) nav
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:nav.viewControllers];
[preferences setObject:encodedObject forKey:navStatusKey];
// Save to disk
return [preferences synchronize];
}
但是,当我回到应用程序中时,苹果统计信息告诉我约束未得到遵守,并且应用程序将崩溃,然后当我在导航堆栈中添加viewControllers时,它确实崩溃了:)
任何提示或帮助将不胜感激。非常感谢
您是否在应用程序委托中实现了application:shouldRestoreApplicationState:
和application: shouldSaveApplicationState:
方法。希望能为您提供帮助:
1.设置恢复标识符
分配恢复标识符时,请记住,视图控制器层次结构中的所有父视图控制器也必须也具有恢复标识符。还包括NavigationController和TabBar…
一种。为视图的restoreIdentifier属性分配一个有效的字符串。
b。使用还具有有效还原标识符的视图控制器中的视图。
C。对于表视图和集合视图,分配一个采用UIDataSourceModelAssociation协议的数据源。
--
2.告诉您要使用状态保存的应用程序
将这两种方法添加到应用程序委托.m文件中,以既需要保存又需要恢复应用程序状态
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
3.写入和读取控制器状态
您的Preservation Controller必须采用UIStateRestoring协议,并使用该协议的方法来写入和读取其状态。
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
if (_textLabel.text.length > 0) {
[coder encodeObject:_textLabel.text forKey:@"labelText"];
}
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
_textLabel.text = [coder decodeObjectForKey:@"labelText"];
[super decodeRestorableStateWithCoder:coder];
}
4.还原ViewController
实现关联的恢复类的viewControllerWithRestorationIdentifierPath:coder:方法来检索视图控制器。
+ (nullable UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder{
ViewControllerThree * restoreVc;
UIStoryboard* storyBoard = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
if (storyBoard) {
restoreVc = (ViewControllerThree*)[storyBoard instantiateViewControllerWithIdentifier:@"ViewControllerThree"];
restoreVc.restorationIdentifier = [identifierComponents lastObject];
restoreVc.restorationClass = [ViewControllerThree class];
}
return restoreVc;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句