Xcode:订购后加载 TabBarItem 标题

发布于 2024-12-06 16:00:12 字数 1928 浏览 2 评论 0原文

我正在使用这个 [教程] 保存用户自定义的 TabBarItems 顺序,因为我的应用程序有超过 5 个选项卡

该教程的概念是将 TabBarItem 标题保存在 NSUserDefault 中,然后加载它们时间应用程序打开。

它非常适合英语,因为我的 TabBarItem 标题最初是在 XIB 文件中设置的,

但问题是,当加载我的应用程序的其他语言时,因为 TabBarItem 标题在启动应用程序时更改为所选语言,

因此当标题保存后重新排序 TabBarItems 的语言与 XIB 文件中设置的标题不同,TabBarItem 在下次应用程序启动时根本不会加载!我认为我使用的教程仅适用于 TabBarItem 标题,当它们与 XIB 文件中定义的 TabBarItem 标题相同时,而不是当这些 TabBarItem 标题根据应用程序语言以编程方式更改时!

- (void)applicationWillTerminate:(UIApplication *)application {
  NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:tabController.viewControllers.count];
  NSArray *tabOrderToSave = tabController.viewControllers;
  for (UIViewController *aViewController in tabOrderToSave) {
    [savedOrder addObject:aViewController.tabBarItem.title];
  }
  [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [self setTabOrderIfSaved];
}

- (void)setTabOrderIfSaved {
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSArray *savedOrder = [defaults arrayForKey:@"savedTabOrder"];
  NSMutableArray *orderedTabs = [NSMutableArray arrayWithCapacity:tabController.viewControllers.count];
  if ([savedOrder count] > 0 ) {
    for (int i = 0; i < [savedOrder count]; i++) {
      for (UIViewController *aController in tabController.viewControllers) {
        if ([aController.tabBarItem.title isEqualToString:[savedOrder objectAtIndex:i]]) {
          aController.tabBarItem.title = NSLocalizedString(aController.tabBarItem.title, nil);
          [orderedTabs addObject:aController];
          }
        }
     }
     tabController.viewControllers = orderedTabs;
  }
}

I'm using this [tutorial] to save user's customized TabBarItems order as my app has more than 5 tabs

The tutorial's concept is to save the TabBarItem titles in the NSUserDefault and load them next time app is opened.

It's working great for english language as my TabBarItem titles are set initially in the XIB file

But the problem is that when my app's other language is loaded, as TabBarItem titles are changed to the selected language upon starting the app

Thus when the titles got saved after re-ordering the TabBarItems for the language that is different than the titles set in the XIB file, the TabBarItem are not loaded at all next time the app started! Which I think the tutorial I used is only work for TabBarItem titles when they are identical to TabBarItem titles defined in XIB file, not when those TabBarItem titles got changed programmatically based on the app language!

- (void)applicationWillTerminate:(UIApplication *)application {
  NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:tabController.viewControllers.count];
  NSArray *tabOrderToSave = tabController.viewControllers;
  for (UIViewController *aViewController in tabOrderToSave) {
    [savedOrder addObject:aViewController.tabBarItem.title];
  }
  [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [self setTabOrderIfSaved];
}

- (void)setTabOrderIfSaved {
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSArray *savedOrder = [defaults arrayForKey:@"savedTabOrder"];
  NSMutableArray *orderedTabs = [NSMutableArray arrayWithCapacity:tabController.viewControllers.count];
  if ([savedOrder count] > 0 ) {
    for (int i = 0; i < [savedOrder count]; i++) {
      for (UIViewController *aController in tabController.viewControllers) {
        if ([aController.tabBarItem.title isEqualToString:[savedOrder objectAtIndex:i]]) {
          aController.tabBarItem.title = NSLocalizedString(aController.tabBarItem.title, nil);
          [orderedTabs addObject:aController];
          }
        }
     }
     tabController.viewControllers = orderedTabs;
  }
}

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

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

发布评论

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

评论(1

树深时见影 2024-12-13 16:00:12

尝试改用 tabBarItem 的 tag 属性。

aController.tabBarItem.tag

Try using the tabBarItem's tag property instead.

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