Add UiViewController to Root View Controller - ios

How do I add the sidebar to self.viewcontroller to init the content.
self.viewController is a rootViewController
- (void)setupViewControllers {
UIViewController *firstViewController = [[BdbHomeListViewController alloc] init];
UIViewController *firstNavigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
UIViewController *secondViewController = [[BdbExpiredListViewController alloc] init];
UIViewController *secondNavigationController = [[UINavigationController alloc]
initWithRootViewController:secondViewController];
UIViewController *thirdViewController = [[BdbHistoryListViewController alloc] init];
UIViewController *thirdNavigationController = [[UINavigationController alloc]
initWithRootViewController:thirdViewController];
UIViewController *forthViewController = [[BdbChatRoomListViewController alloc] init];
UIViewController *forthNavigationController = [[UINavigationController alloc]
initWithRootViewController:forthViewController];
UIViewController *fithViewController = [[RightViewController alloc]init];
UIViewController *fithNavigationController = [[UINavigationController alloc]
initWithRootViewController:fithViewController];
RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
[tabBarController setViewControllers:#[firstNavigationController, secondNavigationController,
thirdNavigationController,forthNavigationController,fithNavigationController]];
TheSidebarController *sidebar = [[TheSidebarController alloc] initWithContentViewController:self.viewController rightSidebarViewController:fithNavigationController];
self.viewController = tabBarController;
[self customizeTabBarForController:tabBarController];
}

I think you should do:
TheSidebarController *sidebar = [[TheSidebarController alloc] initWithContentViewController:tabBarController rightSidebarViewController:fithNavigationController];
self.window.rootViewController= sidebar;
[self.window makeKeyAndVisible];
Also you don't need this:
self.viewController = tabBarController;

Related

How to programmatically add a UITabBarController & UINavigationController in AppDelegate?

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

How to show tab bar in all views

Following is my code and it is showing tab bar only on given 2 tab bar items but it is not showing tab bar in other views.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.startQuizViewController = [[StartQuizViewController alloc] initWithNibName:nil bundle:nil];
self.scoreViewController = [[ScoreViewController alloc] initWithNibName:nil bundle:nil];
self.startQuizViewController.title = #"QUIZ";
self.scoreViewController.title = #"SCORES";
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.startQuizViewController,self.scoreViewController,nil];
_navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];
[self.window addSubview:self.navigationController.view];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
code in written in didFinishLaunchingWithOptions in AppDelegate.m.
change this part:
_navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];
[self.window addSubview:self.navigationController.view];
self.window.rootViewController = self.navigationController;
to this:
self.window.rootViewController = self.tabBarController;
you cannot have tabbarcontroller inside a navigation controller. so just remove navigation controller and set tab bar controller as root view controller
edit:
self.startQuizViewController = [[StartQuizViewController alloc] initWithNibName:nil bundle:nil];
self.scoreViewController = [[ScoreViewController alloc] initWithNibName:nil bundle:nil];
self.startQuizViewController.title = #"QUIZ";
self.scoreViewController.title = #"SCORES";
self.tabBarController = [[UITabBarController alloc] init];
UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:self.startQuizViewController];
UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:self.scoreViewController];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
that's it. you can now push view controller inside those controller and can have the tab bar always there.
Use this
UIViewController *viewController1, *viewController2;
viewController1 = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] ;
UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:viewController1] ;
viewController2 = [[UIViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] ;
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] initWithRootViewController:viewController2] ;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller, navigationcontroller2, nil];
and finally
self.window.rootViewController = self.tabBarController;
This way you have seperate navigation controllers for both tabbar controller.

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

Put a navigation controller in a tabbar controller

I have tried to add a navigation controller to a tab bar controller but the tab appear black.
My code:
AppDelegate.m
#import "SettingsNavigationControllerViewController.h"
#import "SettingsViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
SettingsNavigationControllerViewController *viewController4;
UINavigationController *navigationController = [UINavigationController alloc];
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController3 = [[ShareViewController alloc] initWithNibName:#"ShareViewController" bundle:nil];
SettingsViewController *settingViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
viewController4 = [[SettingsNavigationControllerViewController alloc] initWithRootViewController:settingViewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, navigationController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
thank you in advance
This is because you added a navigationcontroller with no RootViewController and even doesnt initialized. Thats why the view appears as black. May be you mean to add viewController4 instead of navigationController . So change your code as below
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
You need to first create the navigation controllers with rootviewcontrollers
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
Then instantiate the Tab Bar controller like this
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
Create Navigation Controllers first.
UINavigationController *nav1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[FirstSteps alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nav2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[Profiles alloc] initWithNibName:#"SecondView" bundle:nil] autorelease];
nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
Then initialize your tabbar controller like this.
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2 ,nil];
self.window.rootViewController=self.tabBarController;
Hope this helps you.
Hey mate please visit the below link for tab bar example.
I think this is good and nice example which would be helpful to you.
https://github.com/alikaragoz/AKTabBarController
You only have to set your controllers name in appDelegate.m file

facebook login with tabBarController

I am trying to add a Facebook Login function on my app. However, somehow it gives an error saying "'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller as a child of view controller:'
".
My app delegate is written as follow. Could anyone give me an advice how I could fix the problem?
AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) UINavigationController *navigationController;
#property (strong, nonatomic) LoginViewController* loginViewController;
#property (strong, nonatomic) FirstViewController *mainViewController;
AppDelegate.m
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController"
bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.loginViewController];
self.navigationController.delegate = self;
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
//mainwindow
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// ViewControllers array
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
[self resetMainViewController];
//homeview
self.mainViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
//adding navigation controller
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
[viewControllers addObject:self.navigationController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
//secondview
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
//adding navigation
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[viewControllers addObject:navController2];
// Thirdview
ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
//adding navigationcontroller
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
[viewControllers addObject:navController3];
//fourthview
FourthViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
//adding navigation controller
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
[viewControllers addObject:navController4];
//fifthviewcontroller
FifthViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
//adding navigation controller
UINavigationController *navController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
[viewControllers addObject:navController5];
//tabbarcontroller
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:viewControllers];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Thank you.
You set self.navigationController to self.window.rootViewController at the beginning, then add it to the viewcontrollers of the self.tabBarController, this is the problem. You should remove all other self.window.rootViewController assignments, and remain the last one.

Resources