Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the UITabBarItem in the UINavigationController or is the delegate (as it seems) really the best place to do it.

like image 645
fuzzygoat Avatar asked Jan 10 '11 15:01

fuzzygoat


3 Answers

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.

like image 174
Marco Peluso Avatar answered Sep 24 '22 16:09

Marco Peluso


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)

like image 27
Kris Van Bael Avatar answered Sep 25 '22 16:09

Kris Van Bael


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.

like image 27
Adam Avatar answered Sep 22 '22 16:09

Adam