开发者

Xcode: Loading TabBarItem titles after ordering

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;
  }
}


Try using the tabBarItem's tag property instead.

aController.tabBarItem.tag
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜