开发者

iPhone - Changing UITabBarItem title after is customized in More section

I have a UITabBar with 8 items in my xib. I have linked those UITabBarItems in my code, so I do the localization inside the viewDidLoad method of m开发者_开发百科y main window UITabBarController. eg. tabBarItem1.title = NSLocalizedString(@"TAB1");

My problem is that after I changed the order in the More-Configure section, the items that were not at the front keep their original non-localized title from the .xib file.

Any help??


You can set all the UITabBar icons in an easy way.You can do this in the viewWillAppear method:

[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"Aitul", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"Aitor", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(@"Eibar", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(@"Primeran", @"comment")];


In each view controller, you could have them set their title with something like

[[self tabBarItem] setTitle:NSLocalizedString(@"TAB1")];

That might help keep your app from getting confused. I think that a tabBarItem is one of things you can just reference (like setting a title in a navigationcontroller).


I point a solution. The ideal could be to change the tabBarItem title on didEndCustomizingViewControllers.

self.selectedIndex = 3;
[self.selectedViewController.tabBarItem setTitle:@"SSDDD"];

That works, but if I use a non static string, for example NSLocalizedString() method, it does not. Also works for localized strings that were in the 4 visible tab items. Seems very weird.


I solve this problems by 2 steps: :)

First, click TabBarItem of UITabBar in Storyboard or .xib.Then, open Attribute inspector and in section Tab bar Item, choice Identifier to CUSTOM.Then, set Tag (in my case: 0, 1, 2) Finally, connect outlet this TabBar to ViewController. In my case:

@property (weak, nonatomic) IBOutlet UITabBar *tabBar;

Second, open ViewController.m in viewDidLoad: method

for (UITabBarItem *tabBarItem in [self.tabBar items]){
    NSInteger tag = tabBarItem.tag;
    switch(tag){
               case 0:
            [tabBarItem setTitle:@"Title 1"];
            break;
               case 1:
            [tabBarItem setTitle:@"Title 2"];
            break;
               case 2:
            [tabBarItem setTitle:@"Title 3"];
            break;
}

P/S: My english write skills is bad. Sorry if it makes you feel uncomfortable

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜