get reference of the application window in the succeeding classes IPHONE app - iphone

I am adding tab controller on my 4th screen. Up to 4th screen my navigation bar is visible
now on the 4th screen when i am adding Tab Controller to the window, navigation bar is getting disappeared...
code written in the tabbedController class is :
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
firstTab.title=#"First";
firstTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
secondTab.title=#"second";
secondTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
tabController.viewControllers = [NSArray arrayWithObjects:
firstTab,
secondTab,
nil];
// self.tabWindow = [[[[UIApplication sharedApplication]keyWindow ]subviews ] lastObject];
//self.tabWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tabWindow = [[UIApplication sharedApplication] keyWindow ];
//self.tabWindow = self.appDelegateAccess.window;
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
// [self.view addSubview:tabsContainer.view];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Please let me know where I am going wrong....
i think I am not referencing the root window for adding the tab controller ..
it it is the case please suggest me the way to take root window for adding tab controller
Thanks

why are you adding the tabbarcontroller in the window. why dont you add it in self.view ?

First of all make object of tabBar in YourAppDelegate. And write following code in following methode
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
firstTab.title=#"First";
firstTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
secondTab.title=#"second";
secondTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
tabController.viewControllers = [NSArray arrayWithObjects:
firstTab,
secondTab,
nil];
/*
Your Other Code
*/
}
Then in your third View controller's .m file import your appDelegate like..
#import "YourAppDelegate.h"
Last Step write following code on selecting a row of your third view controller...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourAppDelegate * appDel = (YourAppDelegate *)[[UIApplication SharedApplication] delegate];
[appDel.window setRootViewController:appDel.tabController];
}

You need to add navigation controller separately,
Try this,
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
UINavigationController *navControllerOne = [[UINavigationController alloc] initWithRootViewController: firstTab];
navControllerOne.navigationBar.tintColor = [UIColor blackColor];
navControllerOne.tabBarItem.image=[UIImage imageNamed:#"star.png"];
navControllerOne.tabBarItem.title = #"First";
UINavigationController *navControllerTwo = [[UINavigationController alloc] initWithRootViewController: secondTab];
navControllerTwo.navigationBar.tintColor = [UIColor blackColor];
navControllerTwo.tabBarItem.image=[UIImage imageNamed:#"star.png"];
navControllerTwo.tabBarItem.title = #"Second";
tabController.viewControllers = [NSArray arrayWithObjects:
navControllerOne,
navControllerTwo,
nil];
// self.tabWindow = [[[[UIApplication sharedApplication]keyWindow ]subviews ] lastObject];
//self.tabWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tabWindow = [[UIApplication sharedApplication] keyWindow ];
//self.tabWindow = self.appDelegateAccess.window;
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
// [self.view addSubview:tabsContainer.view];

great !!!! it fixed ...
I have removed
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
and added only one line
[self.view addSubview:tabController.view];
Finally it worked !!!
Thanks Rohit ..

Related

Creating TabBar programmatically only for one ViewController not in AppDelegate

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

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.

Unbalanced calls to begin/end appearance transitions for <ViewController>,<Tab>

I am trying to implement Tabs into my secondView.On button touch(from ViewController.m) I am navigating to secondView(Tabs). In my Tabs.xib file I have added a TabBar at bottom and it is custom class of UITabBar.
ViewController.m
- (IBAction)touchedInside:(id)sender {
NSLog(#"touhced up inside");
Tabs *temp = [[Tabs alloc]initWithNibName:#"Tabs" bundle:nil];
[self.navigationController pushViewController:temp animated:YES];
[self presentViewController:temp animated:YES completion:nil];
}
Tabs.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
/*
Tab_1 *firstView = [[Tab_1 alloc] init];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:#"First" image:nil tag:1];
[firstView setTabBarItem:item1];
NSLog(#"after first tab is added");
Tab_2 *secondView = [[Tab_2 alloc] init];
UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:#"Sec" image:nil tag:1] ;
[secondView setTabBarItem:item2];
NSLog(#"after second tab is added");
[tabBarController setViewControllers:[NSArray arrayWithObjects:firstView,secondView,nil] animated:NO];
NSLog(#"after tab is added");
[appDelegate.window addSubview:tabBarController.view];
NSLog(#"after view is added");
*/
appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[Tab_1 alloc] initWithNibName:#"Tab 1" bundle:nil];
UIViewController *viewController2 = [[Tab_2 alloc] initWithNibName:#"Tab 2" bundle:nil];
UIViewController *viewController3 = [[Tab_2 alloc] initWithNibName:#"Tab 1" bundle:nil];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];
appDelegate.window.rootViewController = self.tabBarController;
[appDelegate.window makeKeyAndVisible];
Tabs *temp = [[Tabs alloc]initWithNibName:#"Tabs" bundle:nil];
[self.navigationController presentModalViewController:temp animated:NO];
}
Errors
Unbalanced calls to begin/end appearance transitions for <ViewController: 0x6833c80>.
2012-12-06 09:57:48.963 demoTabs[667:f803] Unbalanced calls to begin/end appearance transitions for <Tabs: 0x6a3fc90>.
Tab_1 *viewController1 = [[Tab_1 alloc] initWithNibName:#"Tab 1" bundle:nil];
Tab_2 *viewController2 = [[Tab_2 alloc] initWithNibName:#"Tab 2" bundle:nil];
Tab_2 *viewController3 = [[Tab_2 alloc] initWithNibName:#"Tab 1" bundle:nil];
Define the instance of viewcontroller as above defined, also make sure to give coorect nib file name against each viewcontroller.

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