【问题标题】:How do I save current tab bar item when user quits and reload to that item when restart?用户退出时如何保存当前标签栏项目并在重新启动时重新加载到该项目?
【发布时间】:2010-10-16 03:10:01
【问题描述】:

我想在用户退出应用程序时保存用户所在的当前选项卡,以便在用户重新进入应用程序时加载该应用程序时突出显示该选项卡。

我假设我在我的应用程序委托中使用以下方法来保存当前选项卡

- (void)applicationWillTerminate:(UIApplication *)application

但我如何才能访问当前选项卡 - 重新加载它的最佳方式是什么?

【问题讨论】:

    标签: ios objective-c iphone cocoa cocoa-touch


    【解决方案1】:

    在 applicationWillTerminate 中,将 tabbarcontroller 的 selectedIndex 保存为您的默认值。

      [[NSUserDefaults standardUserDefaults] setInteger:[tabBarController selectedIndex] forKey:@"tabBarIndex"];
    

    然后在启动时,从 NSDefaults 中读取索引,然后设置选项卡。

        setIndex = [[NSUserDefaults standardUserDefaults] objectForKey:@"tabBarIndex"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    

    setIndex 是一个 NSUInteger。然后像这样在 viewDidLoad 中设置 TabBarController:

    [tabBarController selectedIndex:setIndex];
    

    这是凭记忆,所以你需要尝试一下,但这是一般方法。

    干杯,乔丹

    【讨论】:

    • 很好用 - 但它应该是 [[NSUserDefaults standardUserDefaults] setInteger:[tabBarController selectedIndex] forKey:@"tabBarIndex"];
    【解决方案2】:

    乔丹的回答对我有用,除了 selectedIndex 是一个属性,而不是一个方法;所以:

    tabBarController.selectedIndex = setIndex;
    

    【讨论】:

      【解决方案3】:

      UITabBarController 有一个属性可以为你提供当前选择的视图控制器的索引;如果您在终止时将其保存到 NSUserDefaults 并在应用程序再次启动时恢复它,这将恢复用户的选择。

      我在这里故意含糊其辞,因为 UITabBarController 和 NSUserDefaults 的详细信息都在文档中,您需要先学会阅读这些内容,然后再向其他人寻求帮助。您需要的所有其他内容都应该在您的 Xcode 文档浏览器中,或者,如果您尚未安装文档,请访问 http://developer.apple.com

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-09
        • 2017-02-27
        • 2010-12-23
        • 1970-01-01
        • 2016-05-04
        • 2015-06-28
        • 1970-01-01
        相关资源
        最近更新 更多