Navigation-based app with TabBar - iphone

I'm using Navigation-based Application Template with Core Data. Could anyone please tell me how to and a TabBar on the bottom of the view. I am using UITableView, so If I add UITabBar as subview, the TabBar is moving along with tableView when scrolling. I would like to switch between views with TabBar, first "segment" of TabBar should open the RootView (NavigationBar with TableView),and second some other view.
Now I did this:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewController = [NSArray arrayWithObject:yourNavigationController];
self.window.rootViewController = tabBarController
[tabBarController release];
that works fine, but how can I add more Items to UITabBar and for each Item some other view? TabBar has now just one Item on which rootView is loaded
Thanks!

Use UITabBarController as your root view controller in your application delegate:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewController = [NSArray arrayWithObject:yourNavigationController];
self.window.rootViewController = tabBarController
[tabBarController release];

Its simple one, just add UITabbarController to your code and then made the first tab controller to be a navigation controller. And point that navigation controller to your controller which has table view that you want to show.

If you are doing it programmatically you can use this:
FirstViewController *first=[FirstViewController alloc]]init];
UINavigationController *nav=[UINavigationController alloc]]initwithRootViewcontroller:first];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewController = [NSArray arrayWithObject:first];
[tabBarController release];

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.

Best practice when combining Tabs with Navigation

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.

Issue with pushing UIViewController on UITabbarController

I have implemented UINavigationController and UITabbarController with each other. I am able to see Navigation bar and Tab bar along with UIViewController.
Problem is when I push any other UIViewController on this controller,that viewcontroller get pushed but Tabbar get disappeared.
Is there any provision to persist that UITabBar along the stack????
below is code I am referring
UIViewController* cont1 = [UIViewController alloc]init];
UIViewController* cont2 = [UIViewController alloc]init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:cont1,cont2,nil]];
Thanks.
You should do it the other way around.
Place an UINavigationController in each of the UIViewController in the UITabbarController.
YourViewController *viewController = [YourViewController alloc] init] autorelease];
UINavigationController *controller = [UINavigationController alloc] initWithRootViewController:viewController] autorelease];
controller.title = #"something";
controller.tabBarItem.image = [UIImage imageNamed:#"xx"];
tabController.viewControllers = [NSArray arrayWithObject:controller];
then you can push sub view controllers into navigation controller with tab bar persistent at the bottom of the screen.

UINavigation Controller and UITabbarController connumdrum

I have searched and searched but as yet still cannot figure this ole problem out! :-(
I have in my main.xib a TabBarController that is setup with five viewControllers.
I am trying to get the first viewcontroller to be a nav controller so that if the first tab is selected I can push and pop views into view.
But for the life of me I cannot get this to work?
I tried this in my app delegate didLaunch method:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstViewController *first = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:[NSBundle mainBundle]];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
NSArray* controllers = [NSArray arrayWithObjects:firstNav, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
for which I see my view displayed but no buttons appear on the tab bar?
Any tip please?? thnx
For that, you have to add View Controllers in TabBar programatically. Like below:
oFirstViewController.title = #"First View";
oFirstViewController.tabBarItem.image = [UIImage imageNamed:#"ico.png"];
UINavigationController *myNavigationController1 = [[UINavigationController alloc] initWithRootViewController:oFirstViewController];
tabBarController.viewControllers = [NSArray arrayWithObjects:myNavigationController1, myNavigationController2, myNavigationController3, myNavigationController4, nil];
This way, you have to add remaining view-controller to your tabbar controller.
Hope it will he helpful to you.
Let me know in case of any difficulty.
Your tabBarController contains only one viewController. So there will be only one tabBarItem available in tabBarController. As there is only one viewController available, that tabBarItem will be selected by default and you can't change the selection. So you don't feel like there is a button. But its there. You can set the title and image for the viewController and you will see the difference.
first.title = #"firstTab";
first.navigationItem.image = [UIImage imageNamed:#"firstTab.png"];
Below is the link that explain how to setup tabbar controller and navigation controller
Link 1
Hope it helps you......
[first.tabBarItem initWithTitle:#"First" image:[UIImage imageNamed:#"first.png"] tag:0];

My viewcontroller (in the navcontroller) I load in my app delegate overlaps the tabbar

I'm having trouble initializing a viewcontroller when the app loads in my app delegate. The viewcontroller loads okay, but it overlaps the tabbar that I have at the bottom. Do I need to create another viewcontroller and have it load into that in the app delegate? I currently have a tabBarController set up in my MainWindow.xib, which contains Navigation controllers and inside those are viewControllers.
Here is my code...
In my didFinishLaunchingWithOptions I have:
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:sub];
[window addSubview:nav.view];
Should it be something like?
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:sub];
[newViewController.view addSubview:nav.view];
Thanks!
If you're trying to use a UITabBarController, each tab has its own root view controller (these can even be UINavigationController objects). Assuming you have a property for self.tabBarController (I think this gets created by default in Xcode if you are doing a tab-bar app), then:
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewContrller" bundle:nil];
nav = [[UINavigationContoller alloc] initWithRootViewController:sub];
[self.tabBarController setViewControllers:[NSArray arrayWithObject:nav] animated:NO];
self.window.rootViewController = self.tabBarController;
// Clean up memory here... only if you don't need references to them
[sub release];
[nav release];
With setViewControllers:animated: you should probably include additional view controllers in the NSArray, otherwise you'll end up with a tab bar only containing one item!
[window addSubview:tabBarController.view]; would be right......
Rather than adding navigation controller using code..... you should add it from Interface Builder.... in interface builder remove the tabBarItem and add a navigationController on it and then set viewController for that navigation controller..........
create an outlet of tabBarController in appDelegate so that you can add it on window. Pleas do not forget to make connection between components in Interface Builder.
Thanks,