如何:在UITabBarController中自定义选项卡时保存选项卡的顺序 [英] How to: Save order of tabs when customizing tabs in UITabBarController

查看:208
本文介绍了如何:在UITabBarController中自定义选项卡时保存选项卡的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查找除了如何保存UITabBarController的Tab键顺序的文档之外的任何其他信息时遇到问题,以便保存用户的自定义以便下次启动应用程序。我已经在网上搜索过,但一直找不到任何博客文章或文章通过正确的代码来执行此操作。

I am having problems finding any other information than the docs for how to save the tab order for my UITabBarController, so that the user's customization is saved for next app launch. I have searched online, but have been unable to find any blog posts or articles that goes through the proper code for doing this.

我意识到我必须使用委托方法对于UITabBarController(didEndCustomizingViewControllers :),但我不确定如何在保存用户想要标签的订单状态方面保持最佳状态。

I realize I have to use the delegate methods for the UITabBarController (didEndCustomizingViewControllers:) but I am not sure how I best approach persistance in terms of saving the state of the order the user wants the tabs in.

有人可以发布一些代码,指出我正确的方向,或者你有一个链接保存的东西? :)

Can someone post some code, point me in the right direction or perhaps you have a link for something saved? :)

谢谢

推荐答案

据你所知对于一些示例代码,我将在这里简单地发布我在应用程序中处理相同任务的方式。

As far as you've asked for some sample code I will simply post here how I dealt with the same task in my app.

快速介绍:我使用的是NIB文件来存储初始 UITabBarController 状态和我的标签彼此不同我只是为 UITabBarItem 分配标签变量分配给每个的对象UIViewController 塞在我的 UITabBarController 中。为了能够准确地跟踪最后选择的选项卡(包括'更多'选项卡),我已经为 UITabBarController的 UITabBarControllerDelegate 实现了以下方法 UINavigationControllerDelegate 。它们是:

Quick intro: I was using a NIB file for storing initial UITabBarController state and to differ my tabs one from another I simply defined tag variables for UITabBarItem objects assigned to each UIViewController stuffed in my UITabBarController. To be able to accurately track last selected tab (including the 'More' one) I've implemented following methods for UITabBarControllerDelegate of my UITabBarController and UINavigationControllerDelegate of its moreNavigationController. Here they are:

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[NSUserDefaults standardUserDefaults] setInteger:mainTabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[NSUserDefaults standardUserDefaults] setInteger:mainTabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"];
}

#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"];
}

这是保存标签顺序的代码:

And here's the code for saving the tabs order:

#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
    int count = mainTabBarController.viewControllers.count;
    NSMutableArray *savedTabsOrderArray = [[NSMutableArray alloc] initWithCapacity:count];
    for (int i = 0; i < count; i ++) {
        [savedTabsOrderArray addObject:[NSNumber numberWithInt:[[[mainTabBarController.viewControllers objectAtIndex:i] tabBarItem] tag]]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:savedTabsOrderArray] forKey:@"tabBarTabsOrder"];
    [savedTabsOrderArray release];
}

正如您所见,我一直在存储选项卡索引的顺序 NSUserDefaults 中的数组。

As you can see I've been storing the order of tabs' indexes in an array in NSUserDefaults.

在应用程序中启动 applicationDidFinishLaunching:方法我使用以下代码重新排序 UIViewControllers

On app's launch in applicationDidFinishLaunching: method I reordered the UIViewControllers using following code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    mainTabBarController.delegate = self;

    int count = mainTabBarController.viewControllers.count;
    NSArray *savedTabsOrderArray = [[userDefaults arrayForKey:@"tabBarTabsOrder"] retain];
    if (savedTabsOrderArray.count == count) {
        BOOL needsReordering = NO;

        NSMutableDictionary *tabsOrderDictionary = [[NSMutableDictionary alloc] initWithCapacity:count];
        for (int i = 0; i < count; i ++) {
            NSNumber *tag = [[NSNumber alloc] initWithInt:[[[mainTabBarController.viewControllers objectAtIndex:i] tabBarItem] tag]];
            [tabsOrderDictionary setObject:[NSNumber numberWithInt:i] forKey:[tag stringValue]];

        if (!needsReordering && ![(NSNumber *)[savedTabsOrderArray objectAtIndex:i] isEqualToNumber:tag]) {
                needsReordering = YES;
            }
        }

        if (needsReordering) {
            NSMutableArray *tabsViewControllers = [[NSMutableArray alloc] initWithCapacity:count];
            for (int i = 0; i < count; i ++) {
                [tabsViewControllers addObject:[mainTabBarController.viewControllers objectAtIndex:
                                                [(NSNumber *)[tabsOrderDictionary objectForKey:
                                                              [(NSNumber *)[savedTabsOrderArray objectAtIndex:i] stringValue]] intValue]]];
            }
            [tabsOrderDictionary release];

            mainTabBarController.viewControllers = [NSArray arrayWithArray:tabsViewControllers];
            [tabsViewControllers release];
        }
    }
    [savedTabsOrderArray release];

    if ([userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"]) {
        if ([userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"] == 2147483647) {
            mainTabBarController.selectedViewController = mainTabBarController.moreNavigationController;
        }
        else {
            mainTabBarController.selectedIndex = [userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"];
        }
    }

    mainTabBarController.moreNavigationController.delegate = self;

    [window addSubview:mainTabBarController.view];
}

这很棘手,可能看起来很奇怪,但不要忘记我的 UITabBarController 是在nib文件中完全创建的。如果你以编程方式构建它,你可能只是按照保存的顺序执行相同操作。

It's quite tricky and may seem strange, but don't forget that my UITabBarController was fully created in a nib file. If you construct it programmatically you may simply do the same but following the saved order.

PS:并且不要忘记同步 NSUserDefaults 当您的应用终止时。

P.S.: and don't forget to synchronize NSUserDefaults when your app terminates.

- (void)applicationWillTerminate:(UIApplication *)application {
    [[NSUserDefaults standardUserDefaults] synchronize];
}

我希望这会有所帮助。如果问题不明确,请做评论并询问。

I hope this will help. If something is not clear please do comment and ask.

这篇关于如何:在UITabBarController中自定义选项卡时保存选项卡的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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