How to programmatically add a UITabBarController & UINavigationController in AppDelegate? - ios

How to add a UINavigationController & UITabBarController programmatically in app delegate.

Don't forget in the AppDelegate.h file to add:
#property (strong, nonatomic) UITabBarController *tabBarController;
Below is the AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.tabBarController = [[UITabBarController alloc] init];
ViewController1 *VC1 = [[ViewController1 alloc] init];
VC1.title = #"Tab Title Here";
UINavigationController *VC1Navigation = [[UINavigationController alloc]
initWithRootViewController:VC1];
ViewController2 *VC2 = [[ViewController2 alloc] init];
VC2.title = #"Tab Title Here";
UINavigationController *VC2Navigation = [[UINavigationController alloc]
initWithRootViewController:VC2];
ViewController3 *VC3 = [[ViewController3 alloc] init];
homeView.title = #"Tab Title Here";
UINavigationController* VC3Navigation = [[UINavigationController alloc]
initWithRootViewController:VC3];
NSArray* controllers = [NSArray arrayWithObjects:VC1Navigation, VC2Navigation, VC3Navigation, nil];
self.tabBarController.viewControllers = controllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

Related

How to set rootViewController for navigation view controller and tabbar controller in AppDelegate

i have this problem. When i set rootViewController for TabbarController, it show correctly. But i set another rootViewController for navigation bar, TabbarController will not able to display. Any idea?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[DCTabBarController alloc] init];
DCTabBarController *tabBar = (DCTabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:2];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:rootNav];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}
After applied Adeel solution, here is the output.But items in tabbar will not display accordingly.
One important thing to mention here is that an application's window can have only one rootViewController (of course). Like I said in my comment as well, you probably want to do something like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
DCTabBarController *tabBar = [[DCTabBarController alloc] init];
[tabBar setViewControllers:#[rootNav]];
self.window.rootViewController = tabBar;
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:tabBar];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}

created Tab bar and side menu Issues

I created tab bar and side menu using Sw reveal programmatically but after implementing all process i cant able to view side menu and my tab bar also disappeared...i don't know how to alloc them pleas help me out
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.tabBarController =[[UITabBarController alloc]init];
//Initialize View controller and speciality
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
self.tabBarController.viewControllers=[NSArray arrayWithObjects:viewcontroller1,viewcontroller2,viewcontroller3,viewcontroller4,viewcontroller5, nil];
self.window.rootViewController =self.tabBarController;
self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000];
self.window.backgroundColor = [UIColor whiteColor];
HomeView *frontViewController = [[HomeView alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
mainRevealController.delegate = self;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You set your window.rootViewController two times. And it seems the second time you assign nil to it, as I can't see any initialization/assignement to self.viewController.

App delegate issues with multiple view controllers

I created tab bar controller and implemented in app delegate but before that i also implemented other view controller.i don't know how to use call them orderly.please tell me how to make them arrange as i like.
#interface AppDelegate ()
{
UINavigationController *navigation;
}
#property (strong, nonatomic) JASidePanelController *viewController;
#property (strong, nonatomic) UITabBarController *this;
#end
#implementation AppDelegate
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigation = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
self.window.rootViewController = navigation;
//UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.window = window;
self.viewController = [[JASidePanelController alloc] init];
self.viewController.shouldDelegateAutorotateToVisiblePanel = NO;
self.viewController.leftPanel = [[LeftViewController alloc] init];
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
viewcontroller1.view.backgroundColor = [UIColor whiteColor];
viewcontroller2.view.backgroundColor = [UIColor whiteColor];
viewcontroller3.view.backgroundColor = [UIColor whiteColor];
viewcontroller4.view.backgroundColor = [UIColor whiteColor];
viewcontroller5.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navcontroller1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
UINavigationController *navcontroller2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
UINavigationController *navcontroller3=[[UINavigationController alloc] initWithRootViewController:viewcontroller3];
UINavigationController *navcontroller4 =[[UINavigationController alloc] initWithRootViewController:viewcontroller4];
UINavigationController *navcontroller5 =[[UINavigationController alloc] initWithRootViewController:viewcontroller5];
viewcontroller1.title = #"Home";
viewcontroller2.title = #"Speciality";
viewcontroller3.title = #"Activity";
viewcontroller4.title = #"Notification";
viewcontroller5.title = #"Profile";
navcontroller1.tabBarItem.image = [UIImage imageNamed:#"home"];
navcontroller2.tabBarItem.image = [UIImage imageNamed:#"special"];
navcontroller3.tabBarItem.image = [UIImage imageNamed:#"activity"];
navcontroller4.tabBarItem.image = [UIImage imageNamed:#"notify"];
navcontroller5.tabBarItem.image = [UIImage imageNamed:#"Pro"];
self.this = [[UITabBarController alloc] init];
self.this.viewControllers=[NSArray arrayWithObjects:navcontroller1,navcontroller2,navcontroller3,navcontroller4,navcontroller5, nil];
self.this.tabBar.barTintColor = RGBCOLOR(249, 178, 131);
self.viewController.centerPanel = _this;
[self.window makeKeyAndVisible];
return YES;
}
Here, you set the window's root controller to a navigation controller:
self.window.rootViewController = navigation;
And then you go on to instantiate a handful of other nav controllers and add them to a tab controller. It's not clear from your question what you're trying to achieve, but if you want your app to use a tab controller, you'll want to set your window's root controller to the tab controller, not a navigation controller.

TabBar and Navigation Controller

Hi I am new to iOS and I am trying to display a Navigation Controller in a TabBar based app.
Here is is what I got:
H file:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
M file:
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3,viewController4,viewController5];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController5];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Instead of adding viewController5 to the tab bar controller's view controllers,, add the navigation controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController5];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3,viewController4,self.navigationController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
This doesn't work. Mixing the view controllers with the navigation controller gives you view controllers without nav and a nav controller without a view controller. You need a nav controller for each vc and then create an array of those nav controllers.
Here is an answer that works: How to add UITabBarController programmatically (no xib file or storyboard)

How to insert UINavigationController inside UITabBarController

How to insert UINavigationController inside UITabBarController.
Currently I have main UITabBarController with declatarion inside application delegate like this (so tab is main)
self.window.rootViewController = self.tabBarController;
And inside one of tabs I want to insert UINavigationController, and can't get it.
The code is contructed like this:
MainWindow.xib with UITabBarController object with tab typed as UINavigationController (point to NavigationHistory.xib) - screenshot: invalid link
NavigationHistory.xib contains only UINavigationController where view point to History.xib
History.xib have only UITableView element - screenshot: invalid link
And now UIViewController doesn't display my View1 view, and I have no clue why it may be. Maybe you have any clue? or point me to the place where such configuration is done.
I'll answer myself. It's good explained at
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html
Write a code in appdelegate.m file.....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSMutableArray *Mutablearray = [[NSMutableArray alloc] init];
UIViewController *1st_View = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:1st_View];
[Mutablarray addObject:Navigation];
UIViewController *2nd_View = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:2nd_View];
[Mutablearray addObject:Navigation];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = Mutablearray;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Add your controller object to UINavigationController, and add that navigationcontroller object to UITabBarController
UINavigationController is a subclass of UIViewController, a UITabBarController expects an array of UIViewControllers (and therefore UINavigationController ); this tells me me that I can give a UITabBarController an array of UINavigationControllers (or sublasses thereof) giving the desired interaction.
Ref : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tab = [[UITabBarController alloc] init];
SimpleTableViewController *tableView = [[SimpleTableViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tableView];
UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:0];
nav1.tabBarItem = item;
AboutViewController *about = [[AboutViewController alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:about];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
nav2.tabBarItem = item2;
tab.viewControllers = #[nav1,nav2];
self.window.rootViewController = tab;
[self.window makeKeyAndVisible];
return YES;
}

Resources