0

我正在尝试在我的应用程序中实现状态保存和恢复机制,因为我在 appDelegate 中实现了以下两种方法,还在情节提要中为我的每个 viewController 提供了恢复 ID,我面临的问题是,当我重新打开应用程序时,我可以看到我以前的或恢复的屏幕,但只有一秒钟,然后它移动到主故事板文件的基本视图控制器。

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
    return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
    return YES;
}

请向我建议解决方案,或者如果我在这里弄错了什么。请耐心等待回复。提前致谢。

更新部分:

我在我的应用程序委托中使用以下方法 NSString * const AppDelegateRootVCKey = @"AppDelegateRootVCKey";

- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.window.rootViewController forKey:AppDelegateRootVCKey];
}

- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {

    // Grabs the preserved root view controller.

    UIViewController * vc = [coder decodeObjectForKey:AppDelegateRootVCKey];

    if (vc) {
        UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        window.rootViewController = vc;
        window.restorationIdentifier = NSStringFromClass([window class]);

        // The green color is just to make it obvious if our view didn't load properly.
        // It can be removed when you are finished debugging.
        window.backgroundColor = [UIColor greenColor];

        self.window = window;
    }
}

我的应用程序初始化部分也在 willFinishLaunchingWithOptions 中,我的 didFinishLaunching 如下

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window becomeFirstResponder];
    return YES;
}

我通过情节提要向所有控制器提供了恢复标识符,也通过代码向我的其他 .xibs 提供了恢复标识符。所以现在上述事情的结果是,每当我在情节提要屏幕上时,它都会保留我的状态,但是如果我点击标题上的后退或其他按钮来打开我的左侧/右侧菜单屏幕,它就会崩溃,原因是无法识别的选择器。崩溃后,如果我再次重新打开它,它会从 mainStoryboard 开始,然后一切正常。另一个问题是,每当我使用我的 xib 屏幕进行测试时,它都不会保留应用程序状态,并从 mainStoryboard 重新启动应用程序。

4

0 回答 0