保存和恢复选项卡栏控制器的状态
我有一个应用程序,它有一个带有两个选项卡的 UITabBarController,每个选项卡都有自己的导航控制器。现在我想在用户关闭应用程序时存储应用程序的状态,以便当用户重新启动应用程序时将显示与上次关闭前相同的位置。
所以,在 applicationWillTerminate: 我有
[NSKeyedArchiver archiveRootObject:tabBarController toFile:@"lastVisitedTab"];
然后,在 applicationDidFinishLaunching: 我有
UITabBarController *last= (UITabBarController *)[NSKeyedUnarchiver unarchiveObjectWithFile:@"lastVisitedTab"];
if (last)
tabBarController = [last retain];
我还有 UIImage 的扩展,以使其符合 NSCoding。然而,这不起作用,因为状态没有被保留。第一个选项卡始终被选中,并且也不保留导航。
有人可以告诉我出了什么问题,或者告诉我如何正确地做吗?
I have an application that has a UITabBarController with two tabs, each having its own navigation controller. Now I want to store the state of the application when the user closes it, so that when the user relauches the application will show the same place as the last time before it was closed.
So, in applicationWillTerminate: I have
[NSKeyedArchiver archiveRootObject:tabBarController toFile:@"lastVisitedTab"];
Then, in applicationDidFinishLaunching: I have
UITabBarController *last= (UITabBarController *)[NSKeyedUnarchiver unarchiveObjectWithFile:@"lastVisitedTab"];
if (last)
tabBarController = [last retain];
I also have an extension to UIImage to make it compliant to NSCoding. However, this doesn't work, as the state is not preserved. The first tab gets selected all the time, and no navigation is preserved either.
Can someone tell me what's wrong, or show me how to do it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为保留实际对象是多余的。相反,只需保存
selectedIndex
属性(使用[NSNumber numberWithInt: tabBar.selectedIndex]
),然后读回并在启动时设置该属性。也许这不能正确回答您的问题,但它可能足以满足您想要实现的目标。I think it's overkill to persist the actual objects. Instead, just save the
selectedIndex
property (use[NSNumber numberWithInt: tabBar.selectedIndex]
) and then read it back and set the property on launch. Maybe this doesn't properly answer your question, but it might be sufficient for what you are trying to achieve.感谢 Felixyz 的想法,我终于弄清楚了如何做到这一点。以下是我必须执行的操作来存储选项卡,无论其数据如何。如果视图加载了从 URL 下载的数据,则存储 URL 而不是整个视图。您必须
在 UIViewController 子类中进行重写,以告诉视图控制器在应用程序停止之前保存适当的数据。
现在,在您的应用程序委托中,在退出之前保存数据
,然后在重新启动时
I figured out how to do it finally, thanks to Felixyz's idea. Below is what I have to do to store tabs, regardless of their data. If, says, a view is loaded with data downloaded from an URL, store the URL instead of the whole view. You would have to override
in your UIViewController subclass to tell the view controller to save appropriate data before the application stops.
Now in your application delegate save the data before quiting
And then, when relaunching