UITabBarController Issue - iphone

I want to create something like below.
RootView does not have TabBar, From the second view there should be TabBar.
What I have currently done is, I am using UINavigationController as controller calass
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
But how can I use UITabBar with tabBarController from SecondViewController?

create the objects of the second view and then push your view with the tabbarcontroller

From Storyboard embed your SecondViewController in TabBar. Select your controller and go to Editor -> Embed in -> TabBar Controller! I'm from my mobile..sorry if i have any mispells!

You need to push your Tabbar controller's object. Initialize your tab bar controller's object and add all other controller objects to the tabbar controller's viewcontroller array.
On button action:-
1> Initialize tab bar controller and suppose you name its object as objTab;
2> objTab.viewcontrollers = [NSArray arrayWithObjects:..] ---> Objects of all viewcontrollers that are a part of your tab bar controller. Thus all objects need to be created first.
3> self.navigationcontroller pushViewController: objTAb

Something like this should do the trick (not using ARC):
//vc1, vc2, vc3 = your view controllers
NSArray *viewControllersArray = [NSArray arrayWithObjects:vc1,vc2,vc3, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllersArray];
[self.navigationController pushViewController:tabBarController animated:YES];
[tabBarController release];
What you want to do is create the UITabBarController and push that along the navigation stack.

For Sample Write loadnewview method at appdelegate .Use buttonPressed method for button action or any object action of first view controller as shown below to display tab bar from second view controller.
I have taken two tabs for sample so I wrote Capacity as 2. You can take up to 5.
-(IBAction)buttonPressed:(id)sender
{
HomeViewController *homeVC=[[HomeViewController alloc]initWithNibName:#"HomeViewController" bundle:nil];
[self.navigationController pushViewController:homeVC animated:YES];
[appDelegate loadnewview];
}
-(void)loadnewview
{
if(!self.tabBarController)
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
NSMutableArray *localcontrollerarray = [[NSMutableArray alloc] initWithCapacity:2];
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[localcontrollerarray addObject:navi1];
UIViewController *viewController2 = [[ScanViewController alloc] initWithNibName:#"ScanViewController" bundle:nil];
UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[localcontrollerarray addObject:navi2];
self.tabBarController.viewControllers = localcontrollerarray;
[self.window addSubview:self.tabBarController.view];
}

use this type of method in AppDelegate.m and property-synthesize UITabBarController and store array of viewcontroller in it also in application didFinishLaunchingWithOptions method just assign navigationViewController as a RootViewController like bellow..
RootViewController *masterViewController = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
after then when you want to add TabBar to any view at that time call this bellow method like this..
[appDelegate addTabBarControllerInwindow];
-(void)addTabBarControllerInwindow
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
[self.navigationController.view removeFromSuperview];
[self.window addSubview:[tabBarController view]];//tabBarController.view
[UIView commitAnimations];
}

Create an Application using Tab Bar Controller and on the ViewDidLoad method of the view controller for which you want to hide the tab bar use code:
[self.tabBarController.tabBar setHidden:YES];
And don't forget to unhide the tab bar using the same code replacing NO instead of YES for the view controller for which you want to show the tab bar.

Related

hide UITabBarController's tab bar when pushing UIViewController

I have a tab bar application.
Here's launching code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
self.tabBarController=[[UITabBarController alloc] init];
StartViewController *startViewController=[[StartViewController alloc] initWithNibName:#"StartViewController" bundle:nil];
NavRootViewController *navRootViewController=[[NavRootViewController alloc] initWithNavControllerWithSubViewController:startViewController];
HelpViewController *helpViewController=[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:nil];
SettingsViewController *settingsViewController=[[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
AboutUsViewController *aboutUsViewController=[[AboutUsViewController alloc] initWithNibName:#"AboutUsViewController" bundle:nil];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects: navRootViewController, helpViewController, settingsViewController, aboutUsViewController, nil]];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController=self.tabBarController;
Application launched with 4 tab bar tabs.
This action is called after user presses start button in the first tab's navigation controller's root view controller
-(IBAction)startPressed:(id)sender
{
NSLog(#"startPressed: called");
RootViewController *vController=[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
[self.navigationController pushViewController:vController animated:YES];
}
This works fine but I need to hide tab bar for my RootViewController
property hidesBottomBarWhenPushed does not work.
Help me please, how can it be done?
I hope this helps you:
- (void)viewWillAppear: (BOOL)animated
{
self.hidesBottomBarWhenPushed = YES;
}
Yea you have to add the modalview on window not on the viewcontroller of tabBar.
Try something like.. make an object of AppDelegate like:
AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
then in next line add
[appDelegate.window.rootviewcontroller.view presentModalViewController:vController animated:YES];
or add your code [self presentModalViewController:vController animated:YES] in the viewDidAppear of the firstviewcontroller of tabBar.
What did you do to solve the problem??I would like to know that also.
If you don't want the main view to show the tab bar, you shouldn't be pushing it onto the navigation controller. Doing this causes the application to assume that this new controller is part of the navigation hierarchy. What is probably the best solution is to start your application on the RootViewController, and then present the navigation controller modally. When you're done with the navigation controller, have it call dismissModalViewController on itself.
Solved using this code:
-(IBAction)startPressed:(id)sender
{
NSLog(#"startPressed: called");
RootViewController *vController=[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc] initWithRootViewController:vController];
[vController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[((AppDelegate *)[UIApplication sharedApplication].delegate).tabBarController presentModalViewController:navController animated:YES];
}
Thanks to #iPhone Developer
UIViewController *nextViewController = [[UIViewController alloc] initWithNibName:#"NextViewController" bundle:[NSBundle mainBundle]];
// hide UITabbarController
nextViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];

How to add a tab bar to flipside view only

I already have the basic code for a utility application, but when the application flips to the second view I want to add a tabbarItem to the bottom of the flipped view only, if the view is flipped back to the original view the tab bar shouldn't show up. How can I add this feature, I'm using simulator 4.1 by the way, thanks!
Here's the code that shows the flipped side when the button is clicked, I want it to flip to a tab bar controller instead.
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
You can hide the UITabbar by using the following code
[yourTabBar setHidden :YES];
and show it using the following code
[yourTabBar setHidden:NO];
By Show/Setup do you mean adding the tabbar controller on the view??Well i am assuming that..You can Add the tabbar controller as a rootviewcontroller of your window i.e. your AppDelegate.Here is the sample Code:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navCon1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navCon2=[[UINavigationController alloc] initWithRootViewController:viewController2];
UIViewController *viewController3=[[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UINavigationController *navCon3=[[UINavigationController alloc] initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navCon1, navCon2,navCon3, nil];
self.window.rootViewController = self.tabBarController;
You're switching between two UIViewControllers, right? Let the second view controller, the one that appears after flipping, be a UITabBarViewController.

How to get navigation bar on ViewBased Application

How to add navigation bar on to ViewBased application [templet]
The following code will present a new navigation controller.
MyViewController *v = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *t = [[UINavigationController alloc] initWithRootViewController:v];
[self presentModalViewController:t animated:true];
[t release];
[v release];
Otherwise, u can add the navigation bar to the nib file associated with the viewcontroller directly.
In your AppDelegate.m-file, add this under applicationDidFinishLaunching (or something like that).
UINavigationBar *navigationBar = [[UINavigationBar alloc] init];
[window addSubview:navigationBar];

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?
Here the viewcontroller Class is is UITableviewcontroller .
right now if i am adding the tab bar it appears some where in table view. i want to display it on bottom of window and and tableview should scroll separately from Tabbarcontroller
Please help and suggest
Thanks
FirstView *view1 = [[FirstView alloc]init];
SecondView *view2 = [[SecondView alloc] init];
UINavigationController *firstview = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *secondview = [[UINavigationController alloc] initWithRootViewController:view2];
UITabBarController* tabBar = [[UITabBarController alloc] init];
[tabBar setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];
[tabBar setDelegate:self];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:tabBar];
[self.view addSubview:navigationController.view];
Try this. here FirstView, SecondView are the class. if you click the tab, corresponding class (view) will open. And you have declare the tabbar and navigation controller delegate.
first you just select tabbar base application from project template.
now double click on mainwindow.xib its open it in interface builder.
now click on tabbarcontroller and open inspector window.
in above image your are able to see list of viewcontrollers. click on class tab will gives you the option of controller type. see in below image
if your are selecting table view controller then your first controller is table view. and tabbar is not in your view. table is scroll separately.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//************ WRITE THE CODE OVER HERE FOR THE CREATION OF THE TAB BAR BY CODING**************
tabbar_object=[[UITabBarController alloc] init];
self.tabbarViewController=[[[TabBarViewController alloc] initWithNibName:#"TabBarViewController" bundle:nil] autorelease];
self.viewcontroller1=[[[FirstViewControllerForTabBar alloc] initWithNibName:#"FirstViewControllerForTabBar" bundle:nil] autorelease];
self.viewcontroller2=[[[SecondViewControllerForTabBar alloc] initWithNibName:#"SecondViewControllerForTabBar" bundle:nil] autorelease];
UINavigationController *tabbatnavigation=[[UINavigationController alloc] initWithRootViewController:tabbarViewController];
tabbatnavigation.title=#"First Tab";
UINavigationController *navigation1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
navigation1.title=#"Sign Up";
UINavigationController *navigation2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
navigation2.title=#"Sign Out";
tabbar_object.viewControllers=[[NSArray alloc] initWithObjects:navigation1,navigation2,tabbatnavigation, nil];
[tabbar_object setSelectedIndex:0];
//************************************************************************************************
self.window.rootViewController = tabbar_object;
[self.window makeKeyAndVisible];
return YES;
}

Implement navigation controller's in a view controller

I have a view controller and I want to implement a navigation controller inside it, but when I implemented it through Interface Builder or programmatically it doesn't work.
Most of it I implemented through Interface Builder, but here is the code I implemented in the AppDelegate, and which I am trying to implement in my view controller.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navController.view];
I know I can't implement "window" in a view controller so I tried this in my view controller:
[self.view addSubview:navController];
But it still doesn't work.
In the app delegate's applicationDidFinishLaunching you have to add this code.
viewController=[[myViewController alloc]init];
navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];
[self.window addSubview:navigationController.view];
where viewController is the object of the myViewController class to which u need to add the navigation controller. It s declared in the header file of appDelegate.Similarly the navigationController is also declared in tat. Hope this helps.
New code:
AppSettings *settings = [[AppSettings alloc] init];
UINavigationController *navCont = [[UINavigationController alloc] initWithRootViewController:settings];
navCont.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController presentModalViewController:navCont animated:YES];
[settings release];
[navCont release];
Try this in your -applicationDidFinishLaunching:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];