如何:获取选项卡顺序,保存到 NSUserDefaults 中然后检索

发布于 2024-12-04 09:40:06 字数 1453 浏览 0 评论 0原文

我有一个带有默认视图和选项卡的UitabBarApplication ...等。我有八个具有可自定义选项的选项卡,因此用户可以重新排序选项卡。

如何获得选项卡的顺序,将其保存到NsuserDefaults中,并在应用程序加载后检索。我想看一些代码示例。

这是我到目前为止的位置:

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:8];
    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:8];
    NSArray *defaultOrder = tabBarController.viewControllers;

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

    tabBarController.viewControllers = orderedLayout;
}

此代码似乎在模拟器中不起作用。我重新排序选项卡并完成完成,然后停止应用程序,但是当我点击构建并再次运行时,应用程序还会恢复为默认情况。为什么上面的代码不起作用?

谢谢。

I have a default UITabBarApplication with default views and tabs... etc. I have eight tabs that have the customizable option, so users can reorder tabs.

How do I get the order of the tabs, save that into NSUserDefaults and retrieve it when the app loads back up. I'd like to see some code examples.

Here is where I'm at so far:

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:8];
    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:8];
    NSArray *defaultOrder = tabBarController.viewControllers;

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

    tabBarController.viewControllers = orderedLayout;
}

This code doesn't seem to work in the simulator. I reorder the tabs and hit done then stop the app, but when I hit build and run again the app reverts to default. Why isn't the code above working?

Thanks.

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

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

发布评论

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

评论(1

已下线请稍等 2024-12-11 09:40:06

一条评论:

NSUserDefaults 并不总是适用于模拟器。如果您每次都进行新的构建,那么当然一切都会重置。

两个建议:

  1. 在后一个方法中放置一个 NSLog 以确保它被调用

  2. 将应用程序构建到设备,移动选项卡,关闭应用程序(完全关闭 - 不仅仅是在后台),再次打开应用程序,看看会发生什么。

One comment:

NSUserDefaults don't always work with the simulator. If you're doing a new build each time, then of course everything gets reset.

Two suggestions:

  1. put an NSLog in the latter method to make sure that it's being called

  2. build the app to a device, move the tabs around, close the app (completely -- not just in background), open the app again, behold what happens.

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