In a UITabBarController, upon selecting a tab, I want that tab's UIViewController to change (assign a new viewcontroller). I'm trying this-
NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];
But it gives error. How to assign a new UIViewController and refresh instantly?
This is based on femina's answer but doesn't require you to build the whole array of view controllers, it just lets you replace an existing view controller with another. In my case I wanted to swap in a different xib file for the iPhone 5 screen:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
Tracking *tracking568h = [[Tracking alloc] initWithNibName:#"Tracking-568h" bundle:nil];
tracking568h.title = [[viewControllers objectAtIndex:0] title];
tracking568h.tabBarItem = [[viewControllers objectAtIndex:0] tabBarItem];
[viewControllers replaceObjectAtIndex:0 withObject:tracking568h];
[tracking568h release];
[self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
This changes the view controller for the first tab, keeping the same tab icon and label.
Please see this code , it offers 2 tabbar's with navigation.
In the AppDelegate.h please declare
UINavigationController *nav1;
UINavigationController *nav2;
UITabBarController *tab;
And in the Appdelegate.m , in the didFinishLaunchingWithOptions please add:-
tab = [[UITabBarController alloc]init];
ViewController *view1 = [[ViewController alloc]init];
nav1= [[UINavigationController alloc]initWithRootViewController:view1];
UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTitle:#"Add" image:[UIImage imageNamed:#"Plus.png"] tag:1];
view1.title = #"Add";
[view1 setTabBarItem:tab1];
SettingsViewController *view2 = [[SettingsViewController alloc]init];
nav2= [[UINavigationController alloc]initWithRootViewController:view2];
UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTitle:#"Setting" image:[UIImage imageNamed:#"settings.png"] tag:2];
view2.title = #"Setting";
[view2 setTabBarItem:tab2];
tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nil];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = tab;
Also check this link for further implementation ... Hope this helps :)
UItabBar changing View Controllers
In the AppDelegate.h
UIViewController *vc1;
UIViewController *vc2;
UIViewController *vc3;
in the Appdelegate.m
in the didFinishLaunchingWithOptions
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
vc1 = [[UIViewController alloc] init];
vc1.title = #"A";
[listOfViewControllers addObject:vc1];
vc2 = [[UIViewController alloc] init];
vc2.title = #"B";
[listOfViewControllers addObject:vc2];
vc3 = [[UIViewController alloc] init];
vc3.title = #"C";
[listOfViewControllers addObject:vc3];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];
Related
I want to create a tabBar in my app but only in a detailView not in the AppDelegate. I am searching how to do this in google but everything what I have figured out is in AppDelegate.m
I am trying to do something like this. This shows me a Tab bar with two buttons(without names) but I want to go from one FirstViewController with tabBar, to one of the two items SecondViewController or ThirdViewController with a navigationBar with one backItemButton for get back to the FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupTabBar];
}
- (void) setupTabBar {
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
YPProfileViewController *profile = [[YPProfileViewController alloc] init];
YPProfileViewController *profile2 = [[YPProfileViewController alloc] init];
[localViewControllersArray addObject:profile];
[localViewControllersArray addObject:profile2];
tabBars.tabBarItem = profile2;
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tabBars.view];
}
at least this works for me.
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[vc1,vc2];
tabBar.selectedIndex = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];
tabbar=[[UITabBarController alloc]init];
NSMutableArray *array = [[NSMutableArray alloc] init];
ViewController * vc=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
tabbar.title=#"Home";
HomeViewController *hvc= [[HomeViewController alloc]initWithNibName:#"HomeViewController" bundle:nil];
hvc.tabBarItem.title=#"Home";
hvc.tabBarItem.image=[UIImage imageNamed:#"home.png"];
SettingViewController* svc =[[SettingViewController alloc]initWithNibName:#"SettingViewController" bundle:nil];
svc.tabBarItem.title=#"My Hospital";
svc.tabBarItem.image=[UIImage imageNamed:#"myhospital.png"];
[array addObject:hvc];
[array addObject:svc];
[array addObject:vc];
tabbar.viewControllers = array;
tabbar.view.autoresizingMask=UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:tabbar.view];
i have written this code tab bar added succesfully but my problem is that it will directly show second window in 1st tab i want 1st view on 1st tab how may i do this thanks in advance..
I think you are trying to give same ViewController instance to first and last tab. May be that is the problem. Once again create another instance for ViewController class and add that class to array.
try this:
tabbar = [[UITabBarController alloc] init];
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
HomeViewController *hvc = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
hvc.tabBarItem.title = #"Home";
hvc.tabBarItem.image = [UIImage imageNamed:#"home.png"];
SettingViewController* svc = [[SettingViewController alloc]initWithNibName:#"SettingViewController" bundle:nil];
ViewController *vc2 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
svc.tabBarItem.title = #"My Hospital";
svc.tabBarItem.image = [UIImage imageNamed:#"myhospital.png"];
tabbar.viewControllers = [NSArray arrayWithObjects: vc, hvc, svc, vc2, nil];
tabbar.view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
tabbar.selectedIndex = 0;
[self.view addSubview:tabbar.view];
your problem is that you have added ViewController * vc, vc twice.
Your code :
[array addObject:vc];
[array addObject:hvc];
[array addObject:svc];
[array addObject:vc]; <- this is wrong.
========================
I seem to have a problem with my tabBar items. Doing the creation of the tabBar I'm able to set this property of the tabBarItem that I want like so:
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
//Create our NavigationViewController object
NavigationViewController *navController = [[NavigationViewController alloc] initWithNibName:#"NavigationViewController" bundle:nil];
//Create our UINavigationController
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:navController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[nav, viewController2, viewController4, viewController3];
nav.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Menukort")
image:[UIImage imageNamed:#"162-receipt.png"]
tag:0];
viewController2.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Favoritter")
image:[UIImage imageNamed:#"28-star.png"]
tag:1];
viewController3.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Info")
image:[UIImage imageNamed:#"104-index-cards.png"]
tag:2];
viewController4.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Kort")
image:[UIImage imageNamed:#"103-map.png"]
tag:3];
viewController2.tabBarItem.badgeValue = #"1";
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
All this code is within a singleton, so sharing objects shouldn't be a problem. Now - how would I set the badgeValue of this tabBarItem in another class?
Thanks in advance.
Get array of tabbarController's viewController using
NSArray *arr = [self.tabbarController viewController];
Then set value for any view controller using
[[[arr objectIndex:1] tabBarItem] setbadgeValue:<nsinteger some value>];
Found the answer myself in another thread.
I did it like this:
[[[[[self tabBarController] tabBar] items]
objectAtIndex:tabIndex] setBadgeValue:badgeValueString];
I am making an application which will have 3 pages
Login Page - first page after the app loads
My First Page- when user successfully logs in then he comes into this page.This page
contains a UITabBar with two UITabBarItems. The first one is connected to
My firstPage
and the other one to My Second Page.
My Second Page - this is another UIViewController.
I have made the login page but I am unable to find the solution to UITabBar adding in My First Page
Please help me out
Define
AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
in AppDelegate.m
didFinishLaunchingWithOptions
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.tabBarController.selectedIndex=0;
self.tabBarController.delegate=self;
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
return YES;
}
now when you get success login write below code in that method
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
delegate.tabBarController = [[UITabBarController alloc] init];
delegate.tabBarController.selectedIndex = 0;
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];
Try this out,
suppose this is LoginViewController.m
-(IBAction)loginButtonClicked:(id)sender
{
[self createTabBarView];
}
//Create TabBar View here
-(void)createTabBarView
{
NSMutableArray *tabItems = [NSMutableArray array];
UIViewController *firstViewController = [[FirstViewController alloc] init];;
firstViewController = #"First View";
firstViewController = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
[tabItems addObject:firstViewController];
UIViewController *secondViewController = [[SecondViewController alloc] init];;
secondViewController = #"Second View";
secondViewController = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
[tabItems addObject:secondViewController];
self.tabBarController = [[UITabBarController alloc]init];
self.tabBarController.viewControllers = tabItems;
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:tabBarController animated:YES];
}
Thanks,
Nikhil.
Are you using interface builder?
From my point of view, I'd rather like to use programmatic way to implement it.
//In the appDidFinishLaunch method
BOOL loggedIn = [[NSUserDefault standDefault]boolForKey:"userlogin"];
if(loggedIn)
{
//Setup your UITabbarViewController
}
else
{
//Setup your loginView Controller
}
After login in LogInViewController
- (void)didLogin
{
YourAppDelegate *delegate = [UIApplication shareApplication].delegate;
[delegate.window addSubView:self.tabBarViewController.view];
[delegate.window removeSubView:self.view]
//Other Config
}
You should create Tabbar at place where you can identify that login is successfully done. This method should be part of your loginViewController.
Create a function like below to create a Tabbar and present it over loginController.
-(void) createTabBarController
{
UITabBarController *tabBar = [[UITabBarController alloc]init];
UIViewController *firstViewController = [[[UIViewController alloc] init]autorelease];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
firstNavController.tabBarItem.title=#"First Controller";
firstNavController.tabBarItem.image = [UIImage imageNamed:#"first.png"];
UIViewController *secondViewController = [[[UIViewController alloc] init]autorelease];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
secondNavController.tabBarItem.title=#"First Controller";
secondNavController.tabBarItem.image = [UIImage imageNamed:#"first.png"];
[tabBar setViewControllers:[NSArray arrayWithObject:firstNavController,secondNavController,nil]];
[firstNavController release];
[secondNavController release];
[self presentModalViewController: tabBar];
[tabBar release];
}
Hai,
I have created a tab based iRestaura project. I use 5 tab bar items. One of the tabbar name is Customer. When I tap on customer another tabbar is created with 3 viewcontroller programmatically. When I use view didload method tabbarcontroller is created successfully. But when i use view didAppear then tabbar controller is not created tabbar. In both of cases the other three viewcontroller which is created programmatically , there is not view did appear is not working. But all of case I need to use viewDidAppear method .
Plz anyone can help me...
The viewDidAppear method which not working is given bellow .....
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UITabBarController *tabBarController1=[[UITabBarController alloc] init];
CustomerListViewController *customerListViewController=[[CustomerListViewController alloc] init];
customerListViewController.title=#"Customer List";
UIImage *anImage1 = [UIImage imageNamed:#"customer.png"];
UITabBarItem *theItem1 = [[UITabBarItem alloc] initWithTitle:#"CustomerList" image:anImage1 tag:0];
customerListViewController.tabBarItem = theItem1;
SelectedCustomerViewController *selectedCustomerViewController= [[SelectedCustomerViewController alloc] init];
selectedCustomerViewController.title=#"Selected Customer";
UIImage *anImage3 = [UIImage imageNamed:#"selectedCustomer.png"];
UITabBarItem *theItem = [[UITabBarItem alloc] initWithTitle:#"Selected Customer" image:anImage3 tag:1];
selectedCustomerViewController.tabBarItem = theItem;
InvoiceListViewController *invoiceListViewController=[[InvoiceListViewController alloc] init];
invoiceListViewController.title=#"Invoice List";
UIImage *anImage2 = [UIImage imageNamed:#"invoiceNo.png"];
UITabBarItem *theItem2 = [[UITabBarItem alloc] initWithTitle:#"Invoice List" image:anImage2 tag:2];
invoiceListViewController.tabBarItem = theItem2;
NSMutableArray *controllers=[[NSMutableArray alloc] init];
[controllers addObject:customerListViewController];
// [controllers1 addObject:vc1];
[controllers addObject:selectedCustomerViewController];
[controllers addObject:invoiceListViewController];
///[controllers1 addObject:vc4];
tabBarController1.viewControllers=controllers;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[[self view] addSubview:tabBarController1.view];
for (int t=0; t<[controllers count]; t++)
{
NSLog(#"controller%#",[controllers objectAtIndex:t]);
}
}
Try this:
Firstly use the below function on your Tabbar controller .m file:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:YES];
}
And in your "viewDidLoad" function of Tabbar controller .m file, use "localNavigationController.delegate = self;" as used in given example below.
UserListing *nearBy = [[UserListing alloc] init];
nearBy.tabBarItem.image = [UIImage imageNamed:#"icoTabNearby.png"];
nearBy.tabBarItem.title = #"Nearby";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:nearBy];
localNavigationController.delegate = self;
Hope it works for you.