Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create tabBarController in appdelegate

I've been following lot's of different tutorials on how to add a UITabBarController programmatically. This would be easy to achieve using storyboard, but since I'm trying to learn how to do things programmatically I can't do that.

At the moment I've got this code in the didFinishLaunchingWithOptions.

tabBarController = [[UITabBarController alloc] init];

NSMutableArray *tabs = [[NSMutableArray alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[MenuViewController alloc] init]];

[tabBarController setViewControllers:tabs];

[tabs addObject:navController];


[self.window addSubview:tabBarController.view];

Edited code:

tabBarController = [[UITabBarController alloc] init];

MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];

This does not do anything to my rootViewController called MenuViewController. How can I achieve this?

like image 670
user3258468 Avatar asked Feb 25 '14 10:02

user3258468


Video Answer


2 Answers

Thie bellow code for 5 tab UITabbarcontroller try with this bellow code:-

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:@"anasayfaViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];

SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:@"SehirRehberiViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:sehirRehberi];

duyuruViewController *duyuru = [[duyuruViewController alloc] initWithNibName:@"duyuruViewController" bundle:nil]; 
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:duyuru];

sikayetViewController *sikayet = [[sikayetViewController alloc] initWithNibName:@"sikayetViewController" bundle:nil];
UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:sikayet];

digerViewController *diger = [[digerViewController alloc] initWithNibName:@"digerViewController" bundle:nil];
UINavigationController *navigationController5 = [[UINavigationController alloc] initWithRootViewController:diger];


self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navigationController1,navigationController2,navigationController3,navigationController4,navigationController5];

 [self.window setRootViewController:tabBarController];
 [self.window makeKeyAndVisible];
like image 165
Nitin Gohel Avatar answered Oct 17 '22 01:10

Nitin Gohel


You should add tab bar controller as a root view controller:

[self.window setRootViewController:tabBarController];

also it's a good idea to first add object to array and after that do something with it, (other way round):

[tabs addObject:navController];
[tabBarController setViewControllers:tabs];
like image 32
Greg Avatar answered Oct 17 '22 00:10

Greg