How to handle the view controllers? - iphone

Hi guys I am working on an application.Where there is a home screen with 5 buttons.On the click of every button i want to open the screen with tabbar with 5 view controllers.I mean when you click on the button the tabbar is opened.And there are back button on the tab bar view as well .On clicking the back button i want to pop back to the home screen and vice versa.
How to do that guys.Any tutorial ,links,sample code would be appreciated.
Thanks a lot to all

Tabbar controller in a navigation stack is complicated. Since each tab controller can have also navigation controller itself.
Have a WindowManager class. It should own both
- FirstViewController and
- TabbarController
All components and the UITabbarControllers themselves should be instantiated in the WindowManager class.
Its init may have code like this, make similar for two tabbarcontrollers.
self.tabBarController = [[UITabBarController alloc] init];
self.controllers = [[NSMutableArray alloc] init];
// initialize the view controllers and navigation controllers for the tab bar
self.friendsVC = [[FriendsVC alloc] initWithNibName:#"FriendsView" bundle:nil];
UINavigationController *friendsNVC = [[UINavigationController alloc] initWithRootViewController: friendsVC];
friendsNVC.navigationBar.barStyle = UIBarStyleBlack;
[controllers addObject:friendsNVC];
[friendsNVC release];
self.paymentsVC = [[PaymentsVC alloc] initWithNibName:#"PaymentsView" bundle:nil];
UINavigationController *paymentsNVC = [[UINavigationController alloc] initWithRootViewController: paymentsVC];
paymentsNVC.navigationBar.barStyle = UIBarStyleBlack;
[controllers addObject:paymentsNVC];
[paymentsNVC release];
tabBarController.viewControllers = controllers;
tabBarController.selectedIndex = 0;
tabBarController.delegate = self;
self.view = tabBarController.view;
In the WindowManager, you can have two methods like,
[WindowManager showViewController] and
[WindowManager showTabbarController].
- showViewController {
//Initiate View controller and use [self.window addSubView:vc.view];
}
- showTabbarController {
// initiate the tabbar manager
}
You can have a "back" button on top of your first tabbar controller left side, to call the
[WindowManager showViewController];

Related

Right design pattern for tabbed navigation views?

I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.
The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.
So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.
This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)
This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).
So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.
UITabBarController *tvc = [[UITabBarController alloc] init];
self.tabBarController = tvc;
[tvc release];
// Instantiates three view-controllers which will be attached to the tabbar.
// Each view-controller is attached as rootviewcontroller in a navigationcontroller.
MainScreenViewController *vc1 = [[MainScreenViewController alloc] init];
PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init];
ExerciseViewController *vc3 = [[ExerciseViewController alloc] init];
UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc1 release];
[vc2 release];
[vc3 release];
nvc1.navigationBar.barStyle = UIBarStyleBlack;
nvc2.navigationBar.barStyle = UIBarStyleBlack;
nvc3.navigationBar.barStyle = UIBarStyleBlack;
NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil];
[nvc1 release];
[nvc2 release];
[nvc3 release];
self.tabBarController.viewControllers = controllers;
[controllers release];
This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).
(this is taken from another part of the app)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.detailedAnswerViewController == nil) {
TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init];
self.detailedAnswerViewController = vc;
[vc release];
}
[self.navigationController pushViewController:self.detailedAnswerViewController animated:YES];
}
The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.

Adding a UITabBarController programmatically with a UINavigationBarController as the first tab TO an existing Navigation Controller

I currently have a UINavigationController in my app delegate where I push a ViewController on to login. If the login is successful I want to then create a UITabBarController with a Navigation Controller as the first Tab whose root controller is a UIViewController that I am creating.
The RootViewController of my first UINavigationController is actually acting as a delegate to the logincontroller so if a user logs in correctly it calls a method in my RootViewController which is where I would then like to push a UITabBarController onto the stack. Here is my code:
UITabBarController *tbController = [[UITabBarController alloc] init];
FileBrowserViewController *fileController = [[FileBrowserViewController alloc] init];
fileController.pathToFileDB = pathToDBUnzipped;
fileController.parentId = #"0";
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fileController];
NSMutableArray *aViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
[aViewControllersArray addObject:navController];
[navController release];
[tbController setViewControllers:aViewControllersArray];
[self.navigationController pushViewController:tbController animated:YES];
[tbController release];
Now, it is all working fine. Except 2 things. Here is the screen shot:
1) I can't see any uitabbar items. How do i set an image and the text for each tab?
2) I don't want that top black bar. I only want 1 bar ontop with the undo button. How do I remove the additional bar?
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.
to hide the above black bar use -
[self.navigationController setNavigationBarHidden:TRUE];
to set tab bar item use -
for system item -
UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
for custom item -
UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTitle:#"title" image:[UIImage imageNamed:#""] tag:0];
[navController setTabBarItem:firstItem];
Here is a good video on how to combine Tab Bar, Navigation Bar, and/or Table Views.
http://www.youtube.com/watch?v=LBnPfAtswgw
If you don't want you sign-up screen to have a Tab Bar controller, then you will have to present it as a modal view (since the tab bar is your root view controller). This can be done through the presentModalViewController:animated: method. You can find info about that at:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html%23//apple_ref/doc/uid/TP40007457-CH111-SW1
I hope that helps. Let me know if you have any other questions!
Cheers, Evan.
hi friend that top bar is status bar . You can set.statusbar hidden = yes;
or change it from plist , when you open your plist there is a option to hide it,

view is hiding tabbarcontroller

I have developed a tab based iphone application.
In this, I am facing a problem as described below:
The view associated with 1st tab bar contains 2-3 buttons. Action of these buttons are to load another view. Now on pressing these buttons the views are loading but in full size(320x480) and hiding the tab bar.
I want to load that view just above the tab bar so that tab bar is accessible.
I explicitly set the view frame in that view's viewDidLoad function, but it is not working.
Please help me out.
Try this :
You need to start with view based application. And then create a UITabbarController in you appDelegate file.
Appdelegate.h
UITabBarController *tabBarController;
// set properties
Appdelegate.m
// Synthsize
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.
Rename the tbs as per your requirement. Place 2 buttons in the one of the views which is Navigation controller.
Hope this helps.
Typical application navigation allows the user to freely move forwards and backwards, between tabs etc. This is facilitated by pushing and popping ViewController's on the navigation bar stack.
In certain scenarios you want to force the user to complete some task and this is when you should use a modal view controller. When the application presents a modal view the idea is that the user should NOT be able to navigate away from the view, instead they should only be able to complete or cancel the action and hence the default behavior for a modal view is to hide the navigation bar, tab bar etc.
It sounds to me from your description that you are performing navigation and not a modal task and thus can I recommend using pushViewController instead of presentModalViewController?
If you are only using presentModalViewController because you want a bottom to top animation then you'll need to use a custom animation.

Navigation controller inside TabViewController

I am editing my question that i had programetically add the tabbar as shown below:-
FirstViewController *obj_FirstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *obj_SecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController *obj_ThirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
navigation1 = [[UINavigationController alloc] initWithRootViewController:obj_FirstViewController];
navigation2 = [[UINavigationController alloc] initWithRootViewController:obj_SecondViewController];
navigation3 = [[UINavigationController alloc] initWithRootViewController:obj_ThirdViewController];
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate=self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:navigation1,navigation2,navigation3,nil]];
MainTabBar.view.frame=self.view.frame;
MainTabBar.selectedIndex=0;
[self.view addSubview:MainTabBar.view]
By writing this in (void)viewDidLoad i got the 3 tab in my viewcontroller.But the problem is i want to set the name of the tab as
Home
Favorites
About us
I had tried by writing the below code:-
obj_FirstViewController.tabBarItem.title=#"Home";
self.title = #"My View Controller";
But this does not work - can anyone please help me how to do this programatically? Where to write the line so that i get this 3 name in my tab bar
this is how I made it
1) I made a view controller with your tabbar
2) added view controllers for each tab and a method to the tabbar view controller class:
-(void)updateContentsWithViewController:(UIViewController *)insideViewController
the method is called when a tabbutton is pressed.
3) here is the code for the switching
- (void)updateContentsWithViewController:(UIViewController *)insideViewController {
//internalViewController is the viewController which you change
[internalViewController.view removeFromSuperview];
[internalViewController release];
internalViewController = [insideViewController retain];
navController = [[UINavigationController alloc] initWithRootViewController:insideViewController];
[navController setNavigationBarHidden:YES];
[navController.view setFrame:CGRectMake(0, 0, 320, 348)];
//frame I needed in my app .. can be changed
[self.internalView addSubview: navController.view];
//navController is the property for the navigationController
[self.view sendSubviewToBack: self.internalView];
}
its possible add tabBar in separate view controller which will be like rootview controller ,then in viewDidLoad of the root view controller assign the object of tabbar created to a appdelegate tabBar declared ...then use appdelegate tabbar for other navigation's alone .

how to create iPhone main menu screen with 2 tabbars subview?

I made two applications that each one of them is tabbar based.
Now I want to combine them to one app that will be struct like that:
-Main menu with 2 buttones.
- button 1: tab bar app no 1.
- button 2: tab bar app no 2.
from each tab bar app, I want an Home button on the left side of the nav bar that will take me to the main menu.
I found this link http://www.pushplay.net/blog_detail.php?id=27 but it's not good to me..
I will be happy if you will able to post some simple code for my problem..
Thanks.
App's 1 & 2 need to be based around viewcontrollers, each of which has a tabbar. The root windows can then just alloc/init and then [self.navigationController pushViewController:viewController animated:YES]; to launch the tabbar controller.
If you have relied on IB to build your tabbar applications, its quite difficult to translate them into view controllers. I create my tabbars programatically (i just find it easier) like this
tabBarController = [[UITabBarController alloc] init]; // creates your tab bar so you can add everything else to it
searchTableViewController = [[SearchTableViewController alloc] init];
UINavigationController *searchTableNavController = [[[UINavigationController alloc] initWithRootViewController:searchTableViewController] autorelease];
[searchTableViewController release];
searchMapViewController = [[SearchMapViewController alloc] init];
UINavigationController *mapTableNavController = [[[UINavigationController alloc] initWithRootViewController:searchMapViewController] autorelease];
[searchMapViewController release];
atestViewController = [[AboutTableViewController alloc] init];
UINavigationController *AboutNavController = [[[UINavigationController alloc] initWithRootViewController:atestViewController] autorelease];
[atestViewController release];
tabBarController.viewControllers = [NSArray arrayWithObjects:searchTableNavController, mapTableNavController, AboutNavController, nil];
[self.view addSubview:tabBarController.view];