如何将选项卡顺序保存到 NSUserDefaults

发布于 2024-12-04 08:10:29 字数 2258 浏览 0 评论 0原文

我有 8 个选项卡,并且正在使用可自定义的 tabBarItems。因此用户可以对选项卡重新排序。现在我的问题是如何获取选项卡的顺序并将其保存到 NSUserDefaults 中,以便每当用户退出应用程序并返回时它都保持不变。

这是我到目前为止得到的代码:

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:8];
    NSArray *tabOrderToSave = tabBarController.viewControllers;
    for (UIViewController *aViewController in tabOrderToSave) {
        [savedOrder addObject:aViewController.title];
    }
    [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
}

该代码没有错误,只是不起作用。

我做错了什么?

顺便说一句:我的应用程序是 tabBarApplication。

编辑: 这是我所做的。

创建了一个 tabBar 应用程序
使用数据填充视图
添加了 8 个选项卡,其中包含从控制器加载的不同视图
然后添加了可自定义的选项卡重新排序
添加了上面的代码来保存 Tab 键顺序


重新启动应用程序时是否可以检索已保存的数据?

编辑2:

我一直在四处寻找,发现了很多关于这方面的信息,但关于如何进行的信息却很少。

这是我现在拥有的代码:

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:6];
    NSArray *savedViews = tabBarController.viewControllers;
    for (UIViewController *theVC in savedViews){
        [vcArray addObject:theVC.title];
    }

    [[NSUserDefaults standardUserDefaults] setObject:vcArray forKey:@"tabLayout"];
}

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *tabLayout = [defaults arrayForKey:@"tabLayout"];
    NSMutableArray *orderedLayout = [NSMutableArray arrayWithCapacity:6];
    NSArray *defaultOrder = tabBarController.viewControllers;

    for (int i =0; i < 6; i++){
        for (UIViewController *theVC in defaultOrder) {
            if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
                [orderedLayout addObject:theVC];
            }
        }
    }

    tabBarController.viewControllers = orderedLayout;
}

为什么这不起作用,我的意思是在我自定义选项卡栏并在 Xcode 中单击停止后,当我再次运行它时,它不会显示我保存的订单。我到底做错了什么?这是执行此操作的正确方法吗?

I have 8 tabs and am using customizable tabBarItems. So the user can reorder the tabs. Now my question is how do I get the order of the tabs and save that to NSUserDefaults so it remains the same whenever the user exits the app and comes back.

Here is the code I've got so far:

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:8];
    NSArray *tabOrderToSave = tabBarController.viewControllers;
    for (UIViewController *aViewController in tabOrderToSave) {
        [savedOrder addObject:aViewController.title];
    }
    [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
}

No errors in that code, it just doesn't work.

What am I doing wrong?

By the way: My app is a tabBarApplication.

EDIT:
Here is what I've done.

Created a tabBar Application
Populated Views with data
Added 8 tabs with different views loaded from controllers
Then added the customizable reordering of tabs
Added the code above to save the tab order

Have do I retrieve that saved data when I relaunch the app?

EDIT 2:

I've been searching around and found a lot of info on this but very little on how.

Here is the code I have now:

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:6];
    NSArray *savedViews = tabBarController.viewControllers;
    for (UIViewController *theVC in savedViews){
        [vcArray addObject:theVC.title];
    }

    [[NSUserDefaults standardUserDefaults] setObject:vcArray forKey:@"tabLayout"];
}

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *tabLayout = [defaults arrayForKey:@"tabLayout"];
    NSMutableArray *orderedLayout = [NSMutableArray arrayWithCapacity:6];
    NSArray *defaultOrder = tabBarController.viewControllers;

    for (int i =0; i < 6; i++){
        for (UIViewController *theVC in defaultOrder) {
            if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
                [orderedLayout addObject:theVC];
            }
        }
    }

    tabBarController.viewControllers = orderedLayout;
}

Why doesn't this work, and by that I mean after I customize the tabbar and hit stop in Xcode, when I go to run it again it doesn't show my saved order. What in the world am I doing wrong? Is this the correct way to do this?

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

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

发布评论

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

评论(3

心房的律动 2024-12-11 08:10:29

你错过了同步。
试试这个:

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:8];
    NSArray *tabOrderToSave = tabBarController.viewControllers;
    for (UIViewController *aViewController in tabOrderToSave) {
        [savedOrder addObject:aViewController.title];
    }
    [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
    [[NSUserDefaults standarduserDefaults] synchronize];
}

You're missing the synchronize.
Try this:

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:8];
    NSArray *tabOrderToSave = tabBarController.viewControllers;
    for (UIViewController *aViewController in tabOrderToSave) {
        [savedOrder addObject:aViewController.title];
    }
    [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
    [[NSUserDefaults standarduserDefaults] synchronize];
}
木落 2024-12-11 08:10:29

正如 @Nekto 所说 - 什么不起作用?你如何检测到这一点?

保存此信息也不会保存您的标签栏顺序...您必须从 nsuserdefault 获取此订单并下次按该顺序生成选项卡..

As @Nekto said - What doesn't work? How do you detect this?

also saving this info won't save your tabbar order... you have to fetch this order from nsuserdefault and generate the tab in that order next time ..

聊慰 2024-12-11 08:10:29

这是我一直在使用的代码

-(void) saveTabOrder
{

    NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:6];
    NSArray *tabOrderToSave    =   tabBarController.viewControllers;
    for (UIViewController *aViewController in tabOrderToSave) 
    {
        [savedOrder addObject:aViewController.title];
    }

    [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
    //[[NSUserDefaults standardUserDefaults] synchronize];

}

// 注意 - 因为选项卡可能会在应用程序版本之间发生变化,
// 如果选项卡数量或名称不应该尝试恢复选项卡
// 与保存的版本不同。

- (void)setTabOrderIfSaved {
    //return;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *savedOrder = [defaults arrayForKey:@"savedTabOrder"];
    NSMutableArray *orderedTabs = [NSMutableArray arrayWithCapacity:6];

    if ([savedOrder count] > 0 )     
    {
        for (int i = 0; i < [savedOrder count]; i++)  // loop through saved tabs
        {
            BOOL tabFound = NO;
            for (UIViewController *aController in tabBarController.viewControllers) // loop through actual tabs
            {
                if ([aController.title isEqualToString:[savedOrder objectAtIndex:i]]) 
                {
                    [orderedTabs addObject:aController];
                    tabFound = YES;
                }
            }
            if (!tabFound) 
            {
                // so the old tab order doesn't include this tab.  Lets bail and use the default ordering
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedTabOrder"];
                return;
            }
        }
        tabBarController.viewControllers = orderedTabs;
    }

}

Here's the code I've been using

-(void) saveTabOrder
{

    NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:6];
    NSArray *tabOrderToSave    =   tabBarController.viewControllers;
    for (UIViewController *aViewController in tabOrderToSave) 
    {
        [savedOrder addObject:aViewController.title];
    }

    [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
    //[[NSUserDefaults standardUserDefaults] synchronize];

}

// NOTE -- because tabs may change between versions of the app,
// should NOT try to restore tabs if the tab count or names
// differ from the saved version.

- (void)setTabOrderIfSaved {
    //return;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *savedOrder = [defaults arrayForKey:@"savedTabOrder"];
    NSMutableArray *orderedTabs = [NSMutableArray arrayWithCapacity:6];

    if ([savedOrder count] > 0 )     
    {
        for (int i = 0; i < [savedOrder count]; i++)  // loop through saved tabs
        {
            BOOL tabFound = NO;
            for (UIViewController *aController in tabBarController.viewControllers) // loop through actual tabs
            {
                if ([aController.title isEqualToString:[savedOrder objectAtIndex:i]]) 
                {
                    [orderedTabs addObject:aController];
                    tabFound = YES;
                }
            }
            if (!tabFound) 
            {
                // so the old tab order doesn't include this tab.  Lets bail and use the default ordering
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedTabOrder"];
                return;
            }
        }
        tabBarController.viewControllers = orderedTabs;
    }

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