Chandu
Chandu

Reputation: 630

Unable to add NavigationBar on TabViewController

I am unable to see NavigationBar on my TabView Controller. I tried to add two tabs. The class hierarchy is like below.

Please help.

ViewControllerOne : UIViewController
ViewControllerTwo : UIViewController
TabViewController : UITabBarController

    ViewControllerOne *viewFirst = [[ViewControllerOne alloc]init];
    ViewControllerOther *viewSecond = [[ViewControllerOther alloc]init];


    [self.tabBarController.navigationController setViewControllers:@[viewFirst, viewSecond]];
    [self.tabBarController.navigationController setTitle:@"TITLE TITLE"];

    viewFirst.title=@"ONE";
    viewFirst.tabBarItem.image = [UIImage imageNamed:@"first.png"];
    viewSecond.title=@"TWO";
    viewSecond.tabBarItem.image = [UIImage imageNamed:@"second.png"];
    viewFirst.view.backgroundColor = [UIColor grayColor];
    viewSecond.view.backgroundColor = [UIColor whiteColor];

    self.viewControllers = [NSArray arrayWithObjects:viewFirst, viewSecond, nil];

Upvotes: 0

Views: 79

Answers (1)

James Richard
James Richard

Reputation: 1535

I'm not sure what "self" is, but it looks like it is just a random view controller. More context on that would be good, but I'll try to throw some thoughts.

It looks like you're trying to set viewFirst and viewSecond as a navigation stack, rather then the two different tabs. It also appears like you aren't placing those controllers within UINavigationControllers. Try something more along these lines:

ViewControllerOne *one = [[ViewControllerOne alloc] init];
one.title = @"ONE";
one.tabBarItem.image = [UIImage imageNamed:@"first.png"];
one.view.backgroundColor = [UIColor grayColor];
UINavigationController *navOne = [[UINavigationController alloc] initWithRootViewController:one];
ViewControllerTwo *two = [[ViewControllerOne alloc] init];
two.title = @"SECOND";
two.tabBarItem.image = [UIImage imageNamed:@"second.png"];
two.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navTwo = [[UINavigationController alloc] initWithRootViewController:two];
TabViewController *tabController = [[TabViewController alloc] init];
[tabController setViewControllers:@[navOne, navTwo] animated:NO];

Make sure that the tabController is the key UIWindow's root view controller.

Upvotes: 1

Related Questions