RootViewController to TabBarViewController - iphone

What is the way to use multiple buttons on root view controller linked with different tab views in Xcode 4.5?
I have got 4 buttons on the home screen, I want the flow goes as when I tap on a button1 it should go to tab1 similarly when I tap on button2 tab2 should be open.
What should I do?
Dummy Code Ignore this.
{
tabBarController.viewControllers = controllers;
window.rootViewController = tabBarController;
}

you can do it like bellow..
First Create object of UITabBarController in AppDelegate class and alloc in applicationDidFinishLaunching: method like bellow..
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController2 = [[[UITabBarController alloc]init]autorelease];
self.tabBarController3 = [[[UITabBarController alloc]init]autorelease];
After create method for set Different Tab as a RootViewController like bellow...
-(void)setRootViewControllerTab1{
UIViewController *viewController1, *viewController2;
UINavigationController *navviewController1 , *navviewController2,;
viewController1 = [[[HomeViewController alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navviewController1.title = #"Title1";
viewController2 = [[[HowItWorksViewController alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navviewController2.title = #"Title2";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
-(void)setRootViewControllerTab2{
UIViewController *viewController3, *viewController4;
UINavigationController *navviewController3 , *navviewController4;
/////TAB 2/////***********
viewController3 = [[[CUHomeViewController alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navviewController3.title = #"Title3";
viewController4 = [[[CUFavouritiesViewController alloc] initWithNibName:#"viewController4" bundle:nil] autorelease];
navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
navviewController4.title = #"Title4";
self.tabBarController2.viewControllers = [NSArray arrayWithObjects:navviewController3, navviewController4, nil];
self.window.rootViewController = self.tabBarController2;
[self.window makeKeyAndVisible];
}
Call above method with AppDelegate object like bellow...
For Example: button1 clicked at that time in method write this code...
- (IBAction)btn1_Clicked:(id)sender{
AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[objApp setRootViewControllerTab1];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController4 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3,viewController4];
self.window.rootViewController = self.tabBarController;
}
Create a tabbar controller like this
Call above method with AppDelegate object like bellow...
For Example: button1 clicked at that time in method write this code...
- (IBAction)btn1_Clicked:(id)sender{
AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
objApp.tabbarcontroller.selectedindex = 2;
}
you can change selectedIndex

Just create 4 viewControllers and append below code in app delegates method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController4 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3,viewController4];
self.window.rootViewController = self.tabBarController;

Related

implementation of tab bar controller

this view present 5 buttons(sorry is in french)
i want to implement a tabbarcontroller when i choose one button , it will navigate to the tabbarControllerClass my problem that i don't know how to program action in every buttton to push to this view
this is an example of mesAlertbuttonpressed
-(IBAction)MesAlertsButtonPressed:(id)sender{
TabBarControllerViewController *tabBarControllerViewController = [[TabBarControllerViewController alloc]initWithNibName:#"TabBarControllerViewController" bundle:nil];
tabBarControllerViewController.TypeView=#"Alert";
[self.navigationController pushViewController:tabBarControllerViewController animated:YES];
Create method in AppDelegate.m class and call that method when you want to display tabController For Example:
-(void)setRootViewControllerTab1{
UIViewController *viewController1, *viewController2, *viewController3, *viewController4, *viewController5;
UINavigationController *navviewController1 , *navviewController2, *navviewController3, *navviewController4, *navviewController5;
viewController1 = [[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil] autorelease];
navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
viewController2 = [[[HowItWorksViewController alloc] initWithNibName:#"HowItWorksViewController" bundle:nil] autorelease];
navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
viewController3 = [[[JoiniAppointViewController alloc] initWithNibName:#"JoiniAppointViewController" bundle:nil] autorelease];
navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
viewController4 = [[[BecomeBussUserViewController alloc] initWithNibName:#"BecomeBussUserViewController" bundle:nil] autorelease];
navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
viewController5 = [[[ContactUsViewController alloc] initWithNibName:#"ContactUsViewController" bundle:nil] autorelease];
navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
and call that method on that button click event like bellow..
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setRootViewControllerTab1];
Use this line of code to call a method to create tab bar-
AppDelegate *appDelegateObj = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegateObj createTabBar];
Actual Method
-(void)createTabBar
{
self.tabBarController.customizableViewControllers = nil;
Home *homeObj = [[Home alloc] initWithNibName:#"Home" bundle:nil];
UINavigationController *tab1Controller = [[UINavigationController alloc] initWithRootViewController:homeObj];
ChatList *chatListObj = [[ChatList alloc] initWithNibName:#"ChatList" bundle:nil];
UINavigationController *tab2Controller = [[UINavigationController alloc] initWithRootViewController:chatListObj];
Settings *settingObj = [[Settings alloc] initWithNibName:#"Settings" bundle:nil];
UINavigationController *tab3Controller = [[UINavigationController alloc] initWithRootViewController:settingObj];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: tab1Controller, tab2Controller,tab3Controller, nil];
self.tabBarController.selectedIndex=0;
self.tabBarController.delegate = self;
self.window.backgroundColor=[UIColor clearColor];
self.tabBarController.view.backgroundColor=[UIColor clearColor];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
I'm not shure if i understand you right.
Is you view already conrolled by an view controller? If not, then your action will not work.
you can try to show it modal:
-(IBAction)MesAlertsButtonPressed:(id)sender
{
TabBarControllerViewController *tabBarControllerViewController = [[TabBarControllerViewController alloc]initWithNibName:#"TabBarControllerViewController" bundle:nil];
tabBarControllerViewController.TypeView=#"Alert";
[self presentModalViewController:tabBarControllerViewController animated:YES];
}
otherwise you should read the docs about UINavigationController.

navigationcontroller title and button

In my tab based application delegate i added the navigation controller as like the following,
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[view1 alloc] initWithNibName:#"view1" bundle:nil];
UIViewController *viewController4 = [[view2 alloc] initWithNibName:#"view2" bundle:nil];
UIViewController *viewController5 = [[view3 alloc] initWithNibName:#"view3" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3,viewController4,viewController5];
navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];
[self.window addSubview:[navigationController view]];
self.window.rootViewController = self.navigationController;
i would not be able to added the title in each view. While adding it doesn showing the title?Pls help me to resolve this issue
use bellow code...
Here i add UINavigationController to every tab and also assign title for tab and also for UINavigationBar like bellow...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController1, *viewController2, *viewController3, *viewController4, *viewController5;
UINavigationController *navviewController1 , *navviewController2, *navviewController3, *navviewController4, *navviewController5;
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[[YourViewController1 alloc] initWithNibName:#"YourViewController1" bundle:nil] autorelease];
navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navviewController1.title = #"Home";
// navviewController1.navigationBarHidden=YES;
viewController2 = [[[YourViewController2 alloc] initWithNibName:#"YourViewController2" bundle:nil] autorelease];
navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
// navviewController2.navigationBarHidden=YES;
navviewController2.title = #"HowItsWork";
viewController3 = [[[YourViewController3 alloc] initWithNibName:#"YourViewController3" bundle:nil] autorelease];
navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
// navviewController3.navigationBarHidden=YES;
navviewController3.title = #"Join Us";
viewController4 = [[[YourViewController4 alloc] initWithNibName:#"YourViewController4" bundle:nil] autorelease];
navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
// navviewController4.navigationBarHidden=YES;
navviewController4.title = #"Become";
viewController5 = [[[YourViewController5 alloc] initWithNibName:#"YourViewController5" bundle:nil] autorelease];
navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
// navviewController4.navigationBarHidden=YES;
navviewController5.title = #"Contact Us";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];
self.window.rootViewController = self.tabBarController;
}
[self.window makeKeyAndVisible];
}
2)OR
Also you can simple assign title in your particular class like bellow...
-(void)viewWillAppear:(BOOL)animated{
self.title = #"yourTitle";
}
i hope this helpful to you...
Go to viewdidload method in every tabbar controller & use line:
navigationController.title=#"hello";
In every viewcontroller viewdidload
self.navigationcontroler.title=#"firstVC";
"title" is the property of UIViewController and UINavigationController is the subclass of UIViewController you can also access it object of UINavigationController, just like navviewController.title = #"myTitle"; and for more detail check Apple Docs. Hope it will help. Cheers
You have added TabBar Controller inside a Navigation Controller so you cannot add title and arises this issue.
Try to add each navigation controller with View controller inside tabbar controller.
I mean add ur ViewController as a root View controller of navigation controller then add to tab Bar controller.
viewController1 = [[[YourViewController1 alloc] initWithNibName:#"YourViewController1" bundle:nil] autorelease];
navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
Now then ad this navigation controller inside to tabbar controller. Do this for all other viewcontrollers

Add a navigation bar in the app delegates

I have a problem in my appdelegate. I want to add a navigation bar, but it doesn't work.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] init];
UniversViewController *universViewController = [[UniversViewController alloc] initWithNibName:#"ProduitsListinTableViewController" bundle:nil];
universViewController.title = #"univers";
CategoriesViewController *categoriesViewController = [[CategoriesViewController alloc] initWithNibName:#"ProduitsListinTableViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:categoriesViewController];
navigationController.title = #"categories";
[navigationController setNavigationBarHidden:NO];
ProduitsListinTableViewController *produitListe = [[ProduitsListinTableViewController alloc] initWithNibName:#"ProduitsListinTableViewController" bundle:nil];
produitListe.title = #"troisième";
_tabBarController.viewControllers = [NSArray arrayWithObjects:universViewController, categoriesViewController, produitListe, nil];
[self.window setRootViewController:_tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
Do I have to add something in UniversViewController, CategoriesViewController and ProduitsListinTableViewController or this is directly in the appdelegate?
Try this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2,*viewController3,*viewController4,*viewController5;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
viewController1 = [[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil] autorelease];
navController=[[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[[SearchViewController alloc] initWithNibName:#"SearchViewController" bundle:nil] autorelease];
navController2=[[UINavigationController alloc] initWithRootViewController:viewController2];
viewController3 = [[[LocationsViewController alloc] initWithNibName:#"LocationsViewController" bundle:nil] autorelease];
navController3=[[UINavigationController alloc] initWithRootViewController:viewController3];
viewController4 = [[[CallViewController alloc] initWithNibName:#"CallViewController" bundle:nil] autorelease];
navController4=[[UINavigationController alloc] initWithRootViewController:viewController4];
viewController5 = [[[MyBookingsViewController alloc] initWithNibName:#"MyBookingsViewController" bundle:nil] autorelease];
navController5=[[UINavigationController alloc] initWithRootViewController:viewController5];
}
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,navController2,navController3,navController4,navController5,nil];
//self.window.rootViewController =self.navController;
self.window.rootViewController=self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I always follow this approach when I have both a UINavigationController and a UITabbarController:
You need to start with a view based application. And then create a UITabbarController in your appDelegate file.
Appdelegate.h
UITabBarController *tabBarController;
// set properties
Appdelegate.m
// Synthesize
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
// Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController
Search * search = [[Search alloc] init];
UINavigationController *searchNav = [[UINavigationController alloc] initWithRootViewController:search];
Nearby* nearby = [[Nearby alloc] init];
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];
Map* map = [[Map alloc] init];
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];
AboutUs* aboutUs = [[AboutUs alloc] init];
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];
Favorites* favorites = [[Favorites alloc] init];
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];
NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
You can accordingly manage in which tab you want to place navigation controller or only a view controller.
Then in each of the view controllers mentioned above you need to implement
- (id)init {}
in which you can set Tab name and image.
I always follow this approach and it never fails. The tabs are always visible. You can make changes according to your code.

add nav controller to tabbed application

Good day. I try to add new tab with navigation controller to my app. I create a new tabbed app (xcode 4.2) and in appdelegate write this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
NavController *navController = [[[NavController alloc] initWithNibName:#"NavController" bundle:nil] autorelease]; //my controller
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, navController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
NavController.h file
#interface NavController : UINavigationController
#end
Here the structure of project
And when I run project it show me empty tab
But in xib file I add lable and buttons
May be I forgot something?
Initialise your navigation controller with some UIViewController that is :
RootViewController *rootViewController = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
NavController *navController = [[[NavController alloc] initWithRootViewController: rootViewController] autorelease];
This might help.
i am sharing you my APP Code about this feature,Hope this can solve your Problem easily
Add this code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *localNavigationController;
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:3];
viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[localControllersArray addObject:localNavigationController];
AlaramClock *aAlaramClock = [[[AlaramClock alloc] initWithNibName:#"AlaramClock" bundle:nil] autorelease];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:aAlaramClock];
[localControllersArray addObject:localNavigationController];
CurrentTime *aCurrentTime = [[[CurrentTime alloc] initWithNibName:#"CurrentTime" bundle:nil] autorelease];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:aCurrentTime];
[localControllersArray addObject:localNavigationController];
Settings *aSettings = [[[Settings alloc] initWithNibName:#"Settings" bundle:nil] autorelease];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:aSettings];
[localControllersArray addObject:localNavigationController];
tabBarController.viewControllers = localControllersArray;
[localControllersArray release];
// Override point for customization after app launch
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
Then U will get this look
after this in Every ViewController.m
ADD this
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
// Custom initialization
self.title=#"Calculate Time";
self.tabBarItem.title = #"Calculate Time";
self.tabBarItem.image=[UIImage imageNamed:#"time_calculate.png"];
}
return self;
}

ZUUIRevealController with UITabBarController instead of UINavigationController

I'm trying to implement the ZUUIRevealController into a project of mine, this app uses a UITabBarController with 3 tabs.
I went through the screencast and sample code multiple times, but I can't figure out why
[self.navigationController.parentViewController respondsToSelector:#selector(revealToggle)]
only responds with false.
In my appdelegate I just create a simple UITabBarController and add is as a rootViewController:
UITableViewController *activityViewController = [[[ActivityViewController alloc] initWithNibName:#"ActivityViewController" bundle:nil] autorelease];
UIViewController *agendaViewController = [[[AgendaViewController alloc] initWithNibName:#"AgendaViewController" bundle:nil] autorelease];
UIViewController *settingsViewController = [[[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil] autorelease];
UINavigationController *activityNavController = [[[UINavigationController alloc] initWithRootViewController:activityViewController] autorelease];
UINavigationController *agendaNavController = [[[UINavigationController alloc] initWithRootViewController:agendaViewController] autorelease];
UINavigationController *settingsNavController = [[[UINavigationController alloc] initWithRootViewController:settingsViewController] autorelease];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:activityNavController, agendaNavController, settingsNavController, nil];
SortViewController *sortViewController = [[SortViewController alloc] init];
ZUUIRevealController *revealController = [[ZUUIRevealController alloc] initWithFrontViewController:self.tabBarController rearViewController:sortViewController];
[sortViewController release];
//self.window.rootViewController = self.tabBarController;
self.window.rootViewController = revealController;
[revealController release];
[self.window makeKeyAndVisible];
But when I do the check in my ActivityViewController, I only get false
if([self.tabBarController.parentViewController respondsToSelector:#selector(revealToggle)])
{
NSLog(#"YAY");
}
else
{
NSLog(#"WRONG");
}
It seems so simple in the screencast, but it looks like I'm missing something.
ZUUIRevealController: https://github.com/pkluz/ZUUIRevealController
just put : in the if statement while you checking for respondsToSelector: like
if([self.tabBarController.parentViewController respondsToSelector:#selector(revealToggle:)])