【问题标题】:Save and restore UINavigation stacks to reconnect保存和恢复 UINavigation 堆栈以重新连接
【发布时间】:2016-11-16 05:26:04
【问题描述】:

我目前很难理解应该使用什么来保存和恢复我的应用状态。

我正在使用情节提要,并且我有很多 ViewController,我想在应用程序终止时保存我的所有导航堆栈,以便在用户重新启动应用程序时能够恢复所有导航。

我使用 UINavigationController 和其中的另一个 UINavigationController 只是为了提供信息。

我找到了这个并一遍又一遍地阅读: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html#//apple_ref/doc/uid/TP40007457-CH28-SW34

(必需)将恢复标识符分配给要保留其配置的视图控制器;请参阅标记视图控制器以进行保存。

(必需)告诉 iOS 如何在启动时创建或定位新的视图控制器对象;请参阅在启动时恢复视图控制器。

然后我在我的所有 ViewControllers 上添加了一个 RestorationId,但我不明白我应该为第二部分做什么,因为当我添加 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];
}

但是当我返回应用程序时,Apple stats 告诉我约束没有受到尊重并且应用程序将崩溃,然后当我在导航堆栈中添加 viewControllers 时它确实崩溃了 :)

任何提示或帮助将不胜感激。 非常感谢

【问题讨论】:

  • 你实现encodeRestorableStateWithCoder:decodeRestorableStateWithCoder:了吗?您也可以查看本教程。它会帮助你。 techotopia.com/index.php/…
  • 我现在做了我可以得到一些恢复,但正如我所说我想在自己杀死应用程序后做同样的过程,但是当用户杀死它时,下次启动时不会启动恢复,那么有没有办法强制呢?
  • 根据上述恢复 viewControllers 状态,当您从拉伸重新启动应用程序时,它不会工作。在这种情况下,Apple 的默认恢复导航状态将不起作用,但您可以在数据库中保存此用户状态并在应用程序打开时通过手动编程逻辑恢复它,但上述问题没有解决方案,但是这个逻辑如果您有 7-8 导航推送,则仅适用于 3-4 导航推送。
  • 我实际上尝试过使用NSUserDefaults,但是当我保存navigationView时,我只是被拒绝^^并且应用程序崩溃。我的推力很大(我认为大约 15)

标签: ios objective-c uinavigationcontroller save restore


【解决方案1】:

您是否在应用程序委托中实现了application:shouldRestoreApplicationState:application: shouldSaveApplicationState: 方法。希望步骤打击可以帮助您:

1.设置恢复标识符

在分配恢复标识符时,请记住视图控制器层次结构中的所有父视图控制器也必须具有恢复标识符。还包括 NavigationController 和 TabBar…

  • 一个。为视图的 restoreIdentifier 属性分配一个有效的字符串。

  • b.使用视图控制器中的视图,该视图也具有有效的恢复标识符。

  • c。对于表视图和集合视图,分配采用 UIDataSourceModelAssociation 协议的数据源。

-

2。告诉应用你想使用状态保存

将这两个方法添加到应用程序的delegate.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;
}

【讨论】:

  • 这实际上非常有用,但它只回答了我的一半问题。因为我只能用它来从已被杀死的后台应用程序中恢复。但我希望我的应用程序在我杀死它后也能这样做。有没有办法以编程方式启动恢复过程?
  • 你不能通过应用切换器自己杀死应用,否则状态恢复根本不起作用。
  • 我们将在哪里实现这个方法?我每个视图控制器? viewControllerWithRestorationIdentifierPath:coder
猜你喜欢
  • 2014-07-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 2017-07-10
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
相关资源
最近更新 更多