Setting UITabBarItem header from UINavigationController?

I installed a UITabBarController with two tabs, one of which is a simple UIViewController and the other is a UINavigationController, using the second UIViewController as its rootController (later this will be used to configure the UITableView). My question is to name the tabs (i.e. set the title of each UITabBarItem)

For the first tab (a simple UIViewController), I added the following (see below) to the -sit method of the controllers.

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

For another tab, I added (see below) to the second initialization of the controllers (rootController).

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

I set the second tabBarItem header in the right place, because currently it is not displayed when I launch the application, the first tab says β€œONE”, the second is empty. Also tabBarItem (above) shows nil when I print a value or look through the debugger.

EDIT_001: added full code from AppDelegate

I can set UITabBarItem from AppDelegate correctly when I first create controllers ready to be added to UITabBarController. But I really wanted to do this in a separate controller - based on methods for accuracy.

 // 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: the solution still updates the UITabBarItems in the application delegate, for example

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

+7
source share
6 answers

You really need to create a tabBarItem yourself, and then assign it to the 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.

+10
source

When starting the tableviewcontroller, it is not yet part of the navigation controller. Therefore, the variable tabbaritem is zero.

Not sure if this works, but you can customize the tabbaritem tableviewcontroller. With little success, the superimposed navigation controller inherits it. (this is how it works with the title property)

+3
source

I took the approach of creating a helper method for this in AppDelegate for the project I was working on. This helped me centralize the logic that sets the icons and headers for the view controllers on the tab bar controller and should only write them 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 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 UIViewControllers array in the UITabBarController.

+2
source

Try it -

in the header file

 @interface yournavController : UIViewController<UITabBarDelegate> 

and in the init method

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

hope this helps

0
source

Like Marco, you need to create an element, customize it as you wish, and then assign it. This is actually in the Apple documentation :)

0
source

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

  • Inside the layout panel (or IB control panel), I set the headers of the tab items not in their title, but in the key line, which is used in all localized Localizable.strings files. In my case, this is a suitable German text. In many other cases, it will be an English or even a unique key of any shape.
  • 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 (top) view controllers are fully initialized, but only the view controller of the first tab is fully loaded. Everything in viewDidLoad, how you tried and how I tried first, is not entirely useful. Setting headers in init methods does not help either because these values ​​are overwritten by the story pane. TabBarItems now has keys as a title, which may not seem too professional. But converting them into a localized equivalent currently does the trick.

0
source

All Articles