I've a view controller having navigation bar in starting.
After some UIViewConrollers I have added a tabbar controller where I've created four tab bars and four view controllers for each tab bar.
Now if I create tabbars with navigation bars related to each tab it shows two navigation bars at top, if create tabbars without navigation bar it will show only one tabbar but I am not able add title, corresponding to each tab view controller, to that navigation bar
Here is the code in which I created the tab bar
EventDetailViewController *firstViewController = [[EventDetailViewController alloc]init];
firstViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Home" image:[UIImage imageNamed:@"Multimedia-Icon-Off.png"] tag:2]autorelease]; MoreInfo *secondViewController = [[MoreInfo alloc]init];
secondViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Info" image:[UIImage imageNamed:@"Review-Icon-Off.png"] tag:0]autorelease];
PlaceInfoViewController *thirdViewController = [[PlaceInfoViewController alloc]init];
thirdViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Lugar" image:[UIImage imageNamed:@"Place-icon-OFF.png"] tag:1]autorelease];
FavoritesViewController *forthViewController = [[FavoritesViewController alloc]init];
forthViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Compartir" image:[UIImage imageNamed:@"Compartir-Icon-Off.png"] tag:3]autorelease];
UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstViewController, secondViewController, thirdViewController, forthViewController, nil];
tabBarController.view.frame = self.view.frame;
[self.view addSubview:tabBarController.view];
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[forthViewController release];
You can obtain a reference to a view controller's navigationItem and set its title.
[[aViewController navigationItem] setTitle:@"My Title"];
Note that you'll want to embed all your view controllers in navigation controllers if you've not already done so. That way they'll actually get navigation bars with navigation items and you won't have to draw any of that yourself.
NSMutableArray *tabs = [NSMutableArray array];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[tabs addObject:nav];
nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[tabs addObject:nav];
...
[tabBarController setViewControllers:tabs];
Edit: Your tab bar controller is inside a navigation controller, so you need to get a reference to the tab bar controller first. Try [[[self tabBarController] navigationItem] setTitle:...] in the viewDidLoad method of your view controllers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With