如何在终止应用程序之前保留 iPhone 应用程序状态?
我开发了一个带有标签栏和导航控制器的 iPhone 应用程序。
目前运行良好。
现在我希望应用程序在退出之前保存其状态。
假设我有 6 个选项卡,如果有来电,那么重新启动应用程序后,我应该看到最后选择的选项卡。
我看到了关于这个主题的几个问题,但看到它们后我更困惑了,
有人能告诉我一个直接的方法吗?
I have developed an iPhone application with tab bar and navigation controllers.
It is working fine for now.
Now I want the application to save its state before quitting.
Suppose I have 6 tabs and If an incoming call comes , so after relaunching the app I should see the tab selected that was lastly selected .
I have seen several questions on this topic but I am more confused after seeing them,
can anyone tell me a straight way to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这个问题,没有“一刀切”的确切答案 - 保存应用程序状态的实现将在很大程度上取决于应用程序的结构,因此任何单一解决方案都不可能适合您的具体情况。
然而,有一些通用的提示和指示应该可以帮助您走上开发良好状态保存解决方案的正确道路:
您通常希望在
applicationWillTerminate 中执行任何操作来保存状态:
在您的应用委托中或通过侦听其他类中的UIApplicationWillTerminateNotification
。查看 NSUserDefaults。 您可以使用 NSUserDefaults 来存储键/值对(与 NSDictionary 类似,尽管您只能存储与首选项或其他信息相关的实现 NSCoding 的对象(在 NSUserDefaults 中)。使用 NSUserDefaults 来存储状态信息。需要注意的是 - 您不应该使用 NSUserDefaults 来存储大量数据,因为这会减慢应用程序的启动时间(因为这些值是在启动时加载的)。
更具体到您的情况...如果您想要做的只是在应用程序之间保留选定的选项卡,您应该实现
applicationWillTerminate:
并使用 NSUserDefaults 来存储选定的选项卡索引。然后,在您的applicationDidFinishLaunching:
实现中,您将检查该 NSUserDefaults 键是否存在,并将 UITabBarController 的选定索引设置为存储的值(如果存在)。如果您还希望能够恢复所选选项卡的导航堆栈,那么您需要存储足够的信息,以便可以准确地重建导航堆栈。从一般意义上讲,您应该在 NSUserDefaults 中存储哪些信息很难量化,因为它与应用程序的结构以及导航堆栈中视图控制器层次结构的可能迭代高度相关。
如果您需要恢复更通用的导航堆栈,包括可能输入的用户数据,您不希望在应用程序关闭时丢失这些数据(例如,如果用户正在创建某种新记录),我建议您将注意力从在应用程序委托级别存储应用程序状态转移到让视图控制器类侦听 UIApplicationWillTerminateNotification 并执行自己的特定状态保存。您还必须确定某种机制,以使用终止时每个视图控制器保存的信息来恢复导航层次结构。这是一项更复杂的技术,根据您的需要,可能没有必要。
不幸的是,这个答案有点模糊,特别是对于更一般和复杂的情况,因为正如我所指出的,这个问题没有通用的解决方案,因为它在很大程度上取决于您的应用程序结构。
下面是使用 NSUserDefaults 的示例: http://robertcarlsen.net/2009/06/19/overly-simplistic- saving-state-in-of-for-iphone-847
There is no "one size fits all", exact answer to this question - the implementation of saving application state will depending greatly on the structure of the application so much so that any single solution is unlikely to fit your specific case.
There are some general tips and pointers, however, that should put you on the right road to developing a good state preserving solution:
You will typically want to perform any operations to save state in
applicationWillTerminate:
in your app delegate or by listening forUIApplicationWillTerminateNotification
in other classes.Look at NSUserDefaults. You can use NSUserDefaults to store key/value pairs (similar to an NSDictionary, though you can only store objects that implement NSCoding in NSUserDefaults) relating to preference or other information. Use NSUserDefaults to store state information. One caveat - you shouldn't use NSUserDefaults for storing large amounts of data, as this will slow down your application's launch time (since the values are loaded on launch).
More specific to your case...if all you want to do is preserve the selected tab between application, you should implement
applicationWillTerminate:
and use NSUserDefaults to store the selected tab index. Then, in yourapplicationDidFinishLaunching:
implementation, you'll check for the existence of that NSUserDefaults key and set the UITabBarController's selected index to the stored value if it exists.If you want to be able to restore, for example, the navigation stack of the selected tab as well, then you'll need to store enough information so that the navigation stack can be accurately reconstructed. What information you should store in NSUserDefaults is hard to quantify in a general sense as it is highly specific to the structure of your application and the possible iterations of view controller hierarchy within the navigation stack.
If you need to restore a more general navigation stack, including possibly entered user data that you don't want to be lost when the application is closed (such as if the user is creating a new record of some kind), I would suggest that you shift your focus away from storing the application state at the app delegate level and instead focus on having your view controller classes listen for
UIApplicationWillTerminateNotification
and do their own specific state saving. You would also have to determine some mechanism for using that information saved per view controller on termination to restore the navigation hierarchy. This is a more complex technique that may not be necessary depending on your needs.Unfortunately this answer is a bit nebulous, especially for the more general and complex cases, because as I noted there is no general solution for this question since it depends so heavily on your application structure.
Here's an example of using NSUserDefaults: http://robertcarlsen.net/2009/06/19/overly-simplistic-saving-state-in-of-for-iphone-847