Best practice when combining Tabs with Navigation - iphone

I used tabbed application template to start my new iPhone project.
I will have 4 tabs.
And I want to nave navigation bar at the top.
So I added this in AppDelegate:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller, viewController2, nil];
And this added navigation bar only at the top of the first ViewController (first tab).
What I want is to add navigation bar for the whole application not just in one view.
This will enable me to jump from one view to another and back across tabs.
So what is the best way to programmatically make one navigation bar for all tab views?
UPDATE
Basically my main question is should I have one navigation bar for all views or each tab should have its own navigation bar?
What is best practice in "iDevelopers" world.

I'd rather have the self.window.rootViewController be a navigationController, and afterwards, just push your tabBarController or
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// create someViewController
[viewControllers addObject:someView];
// create someViewController2
[viewControllers addObject:someView2];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tabController]
self.window.rootViewController = navController;

If you want to have navigation bars on each controller, you should to have UINavigationController on each tab of your UITabBarController
Look an answer to this question.

You you need navigation bar you have to add UINavigationController as you did for each view controller in tab bar. So self.tabBarController.viewControllers will have all navigation controllers.
Its not good idea to switch across tabs without user intervention.

Related

TabbedView not displaying the Navigation bar

iOS beginner here. I'm Using XCode 4.6.3 and doing some tutorials. I have a question regarding a TabbedView not displaying the Navigation bar:
I set the Top Bar attribute to "Navigation Bar" here :
But it doesn't show here :
Below is the code in the AppDelegate :
self.navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.navController.navigationBar.barStyle = UIBarStyleBlack;
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
What am I doing wrong?
You have initialized your Navigation Controller with your First View Controller. So, you have to use Navigation Controller for the Tab Bar's View Controllers.
Change this Line
self.tabBarController.viewControllers = #[viewController1, viewController2];
With
self.tabBarController.viewControllers = #[self.navController, viewController2];
You have UITabBarController as rootViewController of your UINavigationController. And UINavigationController as root Controller of your app. Instead of that you have to set UITabBarController as root Controller of your App and add UINavigationController in each tab.
Check this answer.

iphone - Tabbar application with NavigationController, without MainWindow.xib

Hihi all,
I am pretty new in iPhone dev. I have follow some tutorial and created a tabbar application. Below is the code in the appdelegate implementation:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UIViewController *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Then set the title and the image for the tab in each of the controller implementation.
My problem is that, example, in my viewController1, I need to navigate to viewController3, when I use presentModalViewController method to push the viewController3 in, the tabbar at the bottom will be disappeared.
While I tried to use the app delegate to refer to my tabBarController, and use tabBarController.navigationController pushViewController method, my viewController3 is not being pushed, and seems nothing happens.
I have tried to follow a few tutorial, but it's all required to drag in the navigationcontroller into the MainWindow.xib, which, in the xcode 4, MainWindow.xib doesn't exist anymore. How can i create the navigationcontroller from code so that the app can navigate between different view without hiding the tabbar?
Any comment is very much appreciated! Thanks in advance!
:)
If you want to use a navigation controller, you need to create a navigation controller. Since you're not using a XIB, you'll have to create it manually.
Since you want the tab bar to remain visible when you present viewController3, you need to make the navigation controller a child of the tab bar controller.
UIViewController *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
navController1,
viewController2,
nil];
Then when you want to present viewController3, do this:
// in some method of viewController1
[self.navigationController pushViewController:viewController3 animated:YES];
I am not very Sure but have you tried this??? Actually i am going to use XCode 4 soon, i am still using 3.2.8 version:-
WebViewController *viewController = [[WebViewController alloc]initWithNibName:#"WebViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];
See in this also the Tab bar will be removed when you navigate to your 3rd screen, you have to provide the navigation bar to come back.
Hope it helps.. :)

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.

Tab bar in view based application?

I am new to iphone development.I have created a view based application.Now i want tab bar in that view.The first view of the tab bar is a table view and the second view is the Web-view.All the tutorials explain only tab bar based application.Since i am using view based application i finding it really hard.How to achieve it by using interface builder.please guide me.Any Sample tutorials will be more useful.Please help me out.Thanks.
Try this one. This code for created a tab bar application in programmatically. So clicked the button it will open the tab bar in the view.
-(IBAction) clkBtn:(id) sender
{
UITabBarController *tabbar1 = [[UITabBarController alloc] init];
firstViewController *first = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: first] autorelease];
secondViewController *second = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: second] autorelease];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,nil];
[self.view insertSubview:tabbar1.view belowSubview: first.view];
[self presentModalViewController:tabbar1 animated:YES];
}
Thanks,
Best of Luck.