应用程序终止时如何保存标签栏顺序? [英] How to save tab bar order when app is terminated?

查看:121
本文介绍了应用程序终止时如何保存标签栏顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例:
http://www.raddonline.com/blogs/geek-journal/iphone-sdk-uitabbarcontroller-how-to-save-user-customized-tab-order/

在iOS4中无效。

我想保存我的标签的顺序

I would like to be able to save the order of my tabs when the application is shut down.

推荐答案

您可以将您的应用程式委托设定为 UITabBarController 委托,并且当选择选项卡时,您可以将选择记录为 [NSUserDefaults standardDefaults] 作为整数(选项卡栏控制器的选定索引)。

You can set your Application delegate to also be a UITabBarController delegate, and when the tab is selected, you can record the selection to [NSUserDefaults standardDefaults] as an integer (selected index of tab bar controller).

执行以下操作:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{
    [[NSUserDefaults standardDefaults] setInteger:tabBarController.selectedIndex forKey:SelectedTabIndex];
}

启动时,您可以使用 -integerForKey:,如果未找到该键的值,或者该键未解析为整数,则它将默认为零。

On launch, you can load the selected index from defaults using -integerForKey: and it will default to zero if a value was not found for that key, or if the key was not parsed as an integer.

我用一个方法包装对保存的索引的访问:

I wrap the access to the saved index with a method:

- (NSUInteger)tabIndexToSelect;
{
    return [[NSUserDefaults standardDefaults] integerForKey:SelectedTabIndex]; // defaults to zero
}






有关 NSUserDefaults 的注释,直到它们同步,它们不会刷新到磁盘的持久性。如果您正在调试和终止应用程序,并且iOS没有自动同步您的应用程序的默认值,您会注意到这一点。你可以强制同步,但我宁愿离开它,直到它自动完成。要帮助用户切换选项卡,然后退出,请同步到 - (void)applicationWillResignActive:(UIApplication *)应用程序和/或 - (void)applicationWillTerminate:(UIApplication *)application


A note about NSUserDefaults.. until they are syncronized, they are not flushed to disk for persistence. You will notice this if you're debugging and terminate the app, and iOS hasn't automatically synchronized your app's defaults. You can force synchronize, but I prefer to leave it until it's done automatically. To help with cases where the user switches tab, and then quits, add synchronize to - (void)applicationWillResignActive:(UIApplication *)application and/or - (void)applicationWillTerminate:(UIApplication *)application.

,而不是选择的索引(我的错!)。

Referring to the tab order, rather than selected index (my bad!).

您可以简单地设置每个标签栏项目具有唯一的标签号。 (使用您的应用程序代理中的枚举来保持它们唯一)。然后你可以这样做:

You can simply set each of your tab bar items with a unique tag number. (use an enumeration in your app delegate to keep them unique). Then you can do this:

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
{
    NSArray *tabBarTagOrder = [tabBarController.tabBar.items valueForKey:@"tag"];
    [[NSUserDefaults standardDefaults] setObject:tabBarTagOrder forKey:TabTagOrder];
}

要恢复启动时的顺序,您可以手动组织选项卡栏项目该顺序从默认值恢复,然后直接设置 UITabBar 属性。

To restore the order at launch, you can manually organize the tab bar items in the order restored from defaults, and then set the items property of the UITabBar directly.

这篇关于应用程序终止时如何保存标签栏顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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