user3258468
user3258468

Reputation: 354

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?

Upvotes: 6

Views: 9088

Answers (4)

Bruno Bieri
Bruno Bieri

Reputation: 10236

If you want to have a UITabBarController as your app's rootViewcontroller you can add this code to the didFinishLaunchingWithOptions function.

It adds a navigation controller containing a list controller and a simple view controller:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
  UITabBarController*     tabBarController           = [[UITabBarController     alloc] init];
  UITableViewController*  myListController           = [[MyListController       alloc] init];
  UINavigationController* navigationControllerMyList = [[UINavigationController alloc] initWithRootViewController:myListController];
  navigationControllerMyList.tabBarItem              = [[UITabBarItem           alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];

  UIViewController* simpleViewController             = [[SimpleViewController   alloc] init];
  simpleViewController.tabBarItem                    = [[UITabBarItem           alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
  tabBarController.viewControllers                   = @[ navigationControllerMyList , simpleViewController ];

  self.window                    = [[UIWindow alloc] init];
  self.window.rootViewController = tabBarController;
  [self.window makeKeyAndVisible];

  return YES;
}

Upvotes: 0

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

UIViewController *viewController_favorites = [[[FavoritesViewController alloc] initWithNibName:@"FavoritesViewController" bundle:nil] autorelease];
UIViewController *viewController_project = [[[ProjectViewController alloc] initWithNibName:@"ProjectViewController" bundle:nil] autorelease];
UIViewController *viewController_search = [[[Search alloc] initWithNibName:@"Search" bundle:nil] autorelease];
UIViewController *viewController_setting = [[[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil] autorelease];

UINavigationController *navController_favorite = [[[UINavigationController alloc] initWithRootViewController:viewController_favorites] autorelease];
UINavigationController *navController_project = [[[UINavigationController alloc] initWithRootViewController:viewController_project] autorelease];
UINavigationController *navController_search = [[[UINavigationController alloc] initWithRootViewController:viewController_search] autorelease];


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController_favorite,navController_project,navController_search,viewController_setting, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49730

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];

Upvotes: 9

Greg
Greg

Reputation: 25459

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];

Upvotes: 1

Related Questions