Tab bar not show my first view - iphone

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.
========================

Related

Setting tabBarItem.badgeValue in other classes?

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

change a view controller of UITabBarController

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

make UITabBar without adding in appDelegate file

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

How to add navigation controller to windowbased app

I have a window based app. I added two viewcontrollers and one Tabbarcontroller to that. Now I want to navigate each viewcontroller to next view. I tried but can't find the solution. Can anyone please help me?
When you add the tab bar on window, add like this-
NSMutableArray * viewControllers = [[NSMutableArray alloc]init];
FirstViewController * firstViewController = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstViewController release];
[viewControllers addObject:nvc];
[nvc release];
SecondViewController * secondViewController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
nvc = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[secondViewController release];
[viewControllers addObject:nvc];
[nvc release];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = viewControllers;
[window addSubview:tabBarController.view];
EDIT -
Whenever you want to navigate in any one of the controller. All you need is to call
[self.navigationController pushViewController:anotherViewController animated:YES];
And you will get exactly what you want. :)
UINavigationController *urNavController = [[UINavigationController alloc] initWithRootViewController:rootController];
If you want to show this on the window
[window addSubview:urNavController.view];
use like this
secondViewController *obj=[[[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil] autorelease];
UINavigationController *navBar=[[UINavigationController alloc] initWithRootViewController:obj];
[self.navigationController pushViewController:navBar animated:YES];
[navBar release];

TabBarController isKindOfClass - problem with UINavigationController

I have a TabBar with ViewController in it. I do this in my AppDelegate. So I have one UINavigationController
test1ViewController = [[Test1ViewController alloc] init];
test2ViewController = [[Test2ViewController alloc] init];
test3ViewController = [[Test3ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: test2ViewController];
NSArray* controllers = [NSArray arrayWithObjects: test1ViewController, navigationController, test3ViewController, nil];
[self.tabBarController setViewControllers:controllers animated:YES];
[navigationController release];
Now I have the problem with this line of source code:
[(Test2ViewController *)[appDelegate.myTabBarController selectedViewController] methodName:arg1 withTag:arg2];
Here there will be a SIGBRT, because the selectedViewController is in this case an "UINavigationController". But I want to call a method of the "Test2ViewController". How could I do this?
Normally I also do this:
if([[appDelegate.myTabBarController selectedViewController] isKindOfClass:[Test2ViewController class]]) { ... }
But this also fail because it is a UINavigationController. How to fix that? Does anyone know?
Thanks a lot in advance & Best Regards.
Try the following:
UINavigationController *navController = (UINavigationController *) [appDelegate.myTabBarController selectedViewController];
Test2ViewController *viewController = (Test2ViewController *) [[navController viewControllers] objectAtIndex: 0];
[viewController methodName:arg1 withTag:arg2];