应用程序终止时如何保存标签栏顺序?

发布于 2024-09-11 04:09:51 字数 335 浏览 1 评论 0原文

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

在 iOS4 中不起作用。

我希望能够在应用程序关闭时保存选项卡的顺序。

The following example:
http://www.raddonline.com/blogs/geek-journal/iphone-sdk-uitabbarcontroller-how-to-save-user-customized-tab-order/

Doesn't work in iOS4.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-09-18 04:09:51

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

执行此操作:

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

启动时,您可以使用 -integerForKey: 从默认值加载所选索引,如果未找到该键的值,或者该键未解析为,则默认值为零一个整数。

我使用以下方法包装对已保存索引的访问:

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

关于 NSUserDefaults 的说明.. 在它们同步之前,它们不会刷新到磁盘以实现持久性。如果您正在调试并终止应用程序,并且 iOS 尚未自动同步应用程序的默认设置,您会注意到这一点。您可以强制同步,但我更喜欢保留它直到它自动完成。为了帮助解决用户切换选项卡然后退出的情况,请将同步添加到 - (void)applicationWillResignActive:(UIApplication *)application 和/或 - (void)applicationWillTerminate:(UIApplication * )应用程序


指的是制表符顺序,而不是选定的索引(我的错!)。

您可以简单地为每个选项卡栏项目设置一个唯一的标签号。 (在应用程序委托中使用枚举来保持它们的唯一性)。然后您可以这样做:

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

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

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).

Do this in:

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

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
}

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];
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文