开发者

Setting UITabBarItem title from UINavigationController?

I have setup a UITabBarController with two tabs, one is a simple UIViewController and the other is a UINavigationController using a second UIViewController as its rootController (this will later be used to setup a UITableView). My question is with regard to naming the tabs (i.e. setting the title of each UITabBarItem)

For the first tab (simple UIViewController) I have added the following (see below) to the controllers -init method.

// FROM -[MapController init]
- (id)init {
    self = [super init];
    if(self) {
        UITabBarItem *tabBarItem = [self tabBarItem];
        [tabBarItem setTitle:@"ONE"];
    }
    return self;
}

For the other tab I have added (see below) to the second controllers init (rootController).

// FROM -[TableController init]
- (id)init {
    self = [super init];
    if(self) {
        UITabBarItem *tabBarItem = [[self navigationController] tabBarItem];
        [tabBarItem setTitle:@"TWO"];
    }
    return self;
}

Am I setting the second tabBarItem title in the right place as currently it is not showing, when I run the application the first tab says "ONE" the second is blank. Also tabBarItem (above) shows nil when I print the value or look via the debugger.

EDIT_001: Added full code from AppDelegate

I can correctly set the UITabBarItem from within the AppDelegate when I first create the controllers, ready for adding to the UITabBarController. But I really wanted to do this in the individual controller -init methods for neatness.

// UITabBarController
UITabBarController *tempRoot = [[UITabBarController alloc] init];
[self setRootController:tempRoot];
[tempRoot release];
NSMutableArray *tabBarControllers = [[NSMutableArray alloc] init];

// UIViewController ONE
MapController *mapController = [[MapController alloc] init];
[tabBarControllers addObject:mapController];
[mapController release];

// UITableView TWO
TableController *rootTableController = [[TableController alloc] init];
UINavigationController *tempNavController = [[UINavigationController alloc] initWithRootViewController:rootTableController];
[rootTableController release];
[tabBarControllers addObject:tempNavController];
[tempNavController release];

[rootController setViewControllers:tabBarControllers];
[tabBarControllers release];

[window addSubview:[rootController view]];
[window makeKeyAndVisible];

EDIT_002: Solution so far, Update UITabBarItems in app delegate e.g.

// TABLE CONTROLLER
TableController *tableController = [[TableController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableController];
[tableController release];
[tabBarControllers addObject:navController];
UITabBarItem *tabBarItem_002 = [navController tabBarItem];
[tabBarItem_002 setTitle:@"TWO"];
[tabBarItem_002 setImage:[UIImage imageNamed:@"GT_TWO.png"]];
[navController release];

EDIT_003: is there a way to set t开发者_高级运维he UITabBarItem in the UINavigationController or is the delegate (as it seems) really the best place to do it.


You actually have to create the tabBarItem yourself and then assign it to the view controller's tabBarItem property. Try this in your init:

UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"TWO"
                                                         image:[UIImage imageNamed:@"GT_TWO.png"]
                                                           tag:0];
self.tabBarItem = tabBarItem;
[tabBarItem release];

Hope this helps.


While init'ing the tableviewcontroller, it is not part of the navigationcontroller yet. That's why the tabbaritem variable is nil.

Not sure if this works, but you can configure the tableviewcontroller's tabbaritem. With a bit of luck the enclosing navigationcontroller will inherit it. (thats how it works with the title property)


I took the approach of creating a helper method to do this in the AppDelegate for a project I've been working on. It helped me to centralize the logic that sets up the icons and titles for the view controllers on the tab bar controller and only have to write it once.

- (void)setIconAndTitleForViewController:(UIViewController *)viewController iconName:(NSString *)iconName titleKey:(NSString *)titleKey
{
    NSString *iconPath = [[NSBundle mainBundle] pathForResource:iconName ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:iconPath];
    viewController.tabBarItem.image = image;
    [image release];
    viewController.title = NSLocalizedString(titleKey, @"");
}

You would then call it similar to:

UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"SomeNibName" bundle:[NSBundle mainBundle]];
[self setIconAndTitleForViewController:viewController iconName:@"AnIcon" titleKey:@"SomeKey"];

Then add the view controller to the array of UIViewControllers on the UITabBarController.


Try this -

in the header file

@interface yournavController : UIViewController<UITabBarDelegate>

and in the init method

 self.navigationController.tabBar.delegate=self;
    [self.navigationController.tabBar setTitle:@"TWO"];

hope it will help


Just like Marco said, you have to instanciate the item, configure it at your will, then assign it. Actually it's in Apple documentation :)


I have exactly the same problem and found your question an the accepted answer quite helpful. However, I came to the following solution:

  1. Within the storyboard (or IB managed tab bar) I have set the tab bar item's titles not to their title but to the key string that is used in all of the localized "Localizable.strings" files. In my case this is the correspoinding German text. In many other cases this will be the english one or even some unique key of any form.
  2. Witin my app delegat in didFinishLaunchingWithOptions I added:

for (UIViewController *vc in self.window.rootViewController.childViewController) { vc.tabBarItem.title = NSLocalizedString(vc.tabBarItem.title, @"Tab Bar Title"); }

At this point in time, the tab bar is fully loaded, all (topmost) view controllers are fully initialized but only the view controller of the first tab is fully loaded. Anything in viewDidLoad, as you tried and as I tried first, is not quite helpful. Setting the titles in the init methods is not helpful either because those values are overwritten by the story board. Now the TabBarItems have the keys as title which may not look too professional. But converting them to the localized equivalent at this point in time does the trick.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜