在重新启动时保留应用程序状态 [英] Preserving application state across restarts

查看:122
本文介绍了在重新启动时保留应用程序状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图通过使用 [NSKeyedArchiver archiveRootObject:toFile:]序列化我的主 UITabBarController code>,但我遇到了困难。



首先,我遇到了 UIImage 因为它不实现 NSCoding 协议,但我解决了通过为 UIImage 创建一个扩展类别,存储和检索原始图像数据。



现在我遇到的问题是,当我从存档中恢复时,我的视图控制器不存在。我在每个选项卡中都有 UINavigationControllers ,当我还原时, UINavigationItem



我看到 UINavigationController viewControllers 属性标记为(非原子,复制)。这是否意味着,当你归档一个<$​​ c $ c> UINavigationController ,它不包括其堆栈的视图控制器?如果是这样,我怎么能解决这个问题?我首先想到我会覆盖 UINavigationController NSCoding 方法,但是这会从NIB文件中拧紧初始化。 p>

我有点受到扰乱,我一直在这个困难保持应用程序状态。我认为这是一个常见的足够的用例,它将是直接实现。我在这里缺少一些东西?

解决方案

@property上的属性与归档行为很少或没有关系如何getter和setter工作)。



此外,只是因为UI类支持NSCoding,并不意味着它可以用于重建状态。大多数时候,他们支持NSCoding,以便可以使用Interface Builder从NIB文件构建和加载它们。如果您考虑在Interface Builder中设置的所有状态位,则任何数量的这些功能可能不受NSCoding类的实现支持。



通常,人们不使用NSCoding来存储应用程序状态,因为应用程序状态是特定于您的应用程序的。



保存应用程序状态通常涉及存储值,这将允许您重新创建状态。即存储所选标签的索引或表示通过导航控制器走过的路径的一系列所选索引。


I've been trying to preserve the state of my iPhone application by serializing my main UITabBarController using [NSKeyedArchiver archiveRootObject:toFile:], but I'm running into difficulties.

First I had a problem with UIImage, since it doesn't implement the NSCoding protocol, but I solved that by making an extension category for UIImage that stores and retrieves the raw image data.

The problem I'm stuck on now is that my view controllers aren't there when I restore from the archive. I have UINavigationControllers in each of my tabs, and when I restore, the UINavigationItems are still there (I can use the Back buttons and so on to change them) but the view controllers are just gone.

I see that UINavigationController's viewControllers property is marked (nonatomic, copy). Does this mean that when you archive a UINavigationController, it doesn't include its stack of view controllers? If so, how can I get around this? I first thought I would override the NSCoding methods for UINavigationController, but this screws up initialization from the NIB file.

I'm a little perturbed that I've been having this much difficulty preserving app state. I figured it was a common enough use case that it would be straightforward to implement. Am I missing something here?

解决方案

The attributes on a @property have little or nothing to do with archiving behavior (they only describe how getters and setters work).

Also, just because UI classes support NSCoding, doesn't mean that it can be used to reconstruct state. Most of the time, they support NSCoding so that they can be constructed and loaded from a NIB file using Interface Builder. If you think about all the bits of state that you can't set in Interface Builder -- any number of these features may not be supported by the class' implementation of NSCoding.

Normally, people do not use NSCoding to store application state because exactly what constitutes application state is specific to your application.

Saving application state normally involves storing values yourself that would allow you to recreate the state. i.e. store the index of a selected tab or a series of selected indices representing the path walked through a navigation controller.

这篇关于在重新启动时保留应用程序状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆