问题描述
我开发了一个带标签栏和导航控制器的iPhone应用程序。
I have developed an iPhone application with tab bar and navigation controllers.
现在工作正常。
现在我希望应用程序在退出之前保存其状态。
Now I want the application to save its state before quitting.
假设我有6个标签,如果有来电,那么重新启动应用后我应该看到选择了最后选中的标签。
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 ?
推荐答案
没有一刀切,这个问题的确切答案 - 保存应用程序状态的实现将大大依赖关于应用程序的结构如此之多,以至于任何一个解决方案都不太可能适合您的特定情况。
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:
-
您通常希望执行任何操作以保存
applicationWillTerminate:
在您的应用委托中,或通过在其他课程中侦听UIApplicationWillTerminateNotification
。
You will typically want to perform any operations to save state in
applicationWillTerminate:
in your app delegate or by listening forUIApplicationWillTerminateNotification
in other classes.
您可以使用NSUserDefaults存储键/值对(类似于NSDictionary,虽然您只能存储在NSUserDefaults中实现与偏好或其他信息相关的NSCoding的对象。使用NSUserDefaults存储状态信息。一个警告 - 您不应该使用NSUserDefaults来存储大量数据,因为这会减慢应用程序的启动时间(因为这些值是在启动时加载的)。
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).
更具体的案例...如果您只想在应用程序之间保留选定的选项卡,则应实现 applicationWillTerminate:
并使用NSUserDefaults存储所选的标签索引。然后,在 applicationDidFinishLaunching:
实现中,您将检查是否存在该NSUserDefaults键,并将UITabBarController的选定索引设置为存储值(如果存在)。
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 your applicationDidFinishLaunching:
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.
如果您希望能够恢复所选标签的导航堆栈,那么您需要存储足够的信息以便导航堆栈可以准确地重建。您应该在NSUserDefaults中存储哪些信息在一般意义上难以量化,因为它非常特定于应用程序的结构以及导航堆栈中视图控制器层次结构的可能迭代。
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.
如果您需要恢复更通用的导航堆栈,包括可能输入的用户数据,您不希望在关闭应用程序时丢失该用户数据(例如,如果用户正在创建某些新记录我建议您将注意力从存储应用程序委托级别的应用程序状态转移,而是专注于让您的视图控制器类监听 UIApplicationWillTerminateNotification
并执行他们的工作拥有特定的国家储蓄。您还必须确定一些机制,以便在终止时使用每个视图控制器保存的信息来恢复导航层次结构。这是一种更复杂的技术,根据您的需要可能没有必要。
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.
这是一个例子使用NSUserDefaults:
Here's an example of using NSUserDefaults: http://robertcarlsen.net/2009/06/19/overly-simplistic-saving-state-in-of-for-iphone-847
这篇关于如何在终止应用程序之前保留iPhone应用程序状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!