Cla*_*lad 7 restore objective-c save uinavigationcontroller ios
我目前很难理解我应该用什么来保存和恢复我的应用状态.
我正在使用故事板,我有一些ViewControllers,我想在应用程序终止时保存所有导航堆栈,以便在用户重新启动应用程序时能够恢复所有导航.
我在其中使用UINavigationController另一个UINavigationController只是为了获取信息.
我发现了这一点并一遍又一遍地阅读:https: //developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html#//apple_ref/doc/uid/TP40007457-CH28-SW34
(必需)将恢复标识符分配给要保留其配置的视图控制器; 请参阅标记视图控制器以进行保留.
(必需)告诉iOS如何在启动时创建或定位新的视图控制器对象; 请参阅在启动时恢复视图控制器.
然后我RestorationId在我的所有ViewControllers上添加了一个,但是我不明白我应该为第二部分做什么,因为当我添加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];
}
Run Code Online (Sandbox Code Playgroud)
但是当我回到应用程序apple stats告诉我约束没有得到尊重并且应用程序将崩溃然后当我在我的导航堆栈中添加viewControllers时它会崩溃:)
任何提示或帮助将不胜感激.非常感谢
您是否 在应用程序委托中实现了application:shouldRestoreApplicationState:和application: shouldSaveApplicationState:方法。希望能为您提供帮助:
1.设置恢复标识符
分配恢复标识符时,请记住,视图控制器层次结构中的所有父视图控制器也必须也具有恢复标识符。还包括NavigationController和TabBar…
一种。为视图的restoreIdentifier属性分配一个有效的字符串。
b。使用还具有有效还原标识符的视图控制器中的视图。
C。对于表视图和集合视图,分配一个采用UIDataSourceModelAssociation协议的数据源。
--
2.告诉应用您要使用状态保存
将这两种方法添加到应用程序proxy.m文件中,以同时保存和恢复应用程序状态
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)