Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting UITabBarItem title from UINavigationController?
    primarykey
    data
    text
    <p>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)</p> <p>For the first tab (simple UIViewController) I have added the following (see below) to the controllers -init method.</p> <pre><code>// FROM -[MapController init] - (id)init { self = [super init]; if(self) { UITabBarItem *tabBarItem = [self tabBarItem]; [tabBarItem setTitle:@"ONE"]; } return self; } </code></pre> <p>For the other tab I have added (see below) to the second controllers init (rootController).</p> <pre><code>// FROM -[TableController init] - (id)init { self = [super init]; if(self) { UITabBarItem *tabBarItem = [[self navigationController] tabBarItem]; [tabBarItem setTitle:@"TWO"]; } return self; } </code></pre> <p>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.</p> <p><strong>EDIT_001: Added full code from AppDelegate</strong></p> <p>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.</p> <pre><code>// 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]; </code></pre> <p><strong>EDIT_002: Solution so far, Update UITabBarItems in app delegate e.g.</strong></p> <pre><code>// 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]; </code></pre> <p><strong>EDIT_003:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload