Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to use TabBar in viewbase application

Hi,
i have an application which is an viewbase application. now i have to add a tabbar at the bottom.
problem is this, that all tutorials i found on net are on navigation base application. so can i implement tabbar in viewbase application. If yes then How???

Muhammad Usman Aleem

iPhone OS 3.0

Posted on Jul 30, 2009 11:00 PM

Reply
29 replies

Jul 31, 2009 12:00 AM in response to MUsman

Are you using a tab bar controller? Most of the functionality of the tab bar is implemented in the controller. However, the tab bar controller must be the root controller. I.e. in order to use a tab bar controller, its view must be added directly to the window. Since the View-Based template already has a root controller, that template isn't intended for use with a tab bar. Either the Tab Bar or Window-Based Application template would be more suitable.

\- Ray

Jul 31, 2009 2:54 AM in response to MUsman

To use a tab bar without UITabBarController, you'll need to implement some of the controller functionality in your own code:

1) In IB, add a tab bar to your content view and add as many tab items as you want;
2) Ctrl-drag from the tab bar to the view controller (e.g. File's Owner), to connect the delegate outlet of the tab bar to the controller;
3) Select each tab bar item and set its Tag number in the Attributes Inspector: 1, 2, 3, ...
4) in Xcode, open the @interface file for the controller class and adopt the UITabBarDelegate Protocol:

@interface BarTestViewController : UIViewController <UITabBarDelegate> {
}
@end

5) In the @implementation for the controller class add this delegate method:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
}

6) Test the app to make sure the selection of each tab item is correctly logged in the Console. E.g.:

[Session started at 2009-07-31 01:11:18 -0700.]
2009-07-31 01:11:22.770 BarTest[22339:20b] didSelectItem: 1
2009-07-31 01:11:25.930 BarTest[22339:20b] didSelectItem: 2
2009-07-31 01:11:27.274 BarTest[22339:20b] didSelectItem: 3

Once your tab bar is working, you'll need to add more xibs and the code to switch the view based on the tab item selected.

Make a new view controller subclass for each tab. Also add one more View XIB file (New File->iPhone OS->User Interfaces) for each tab. Change the Class of each File's Owner to the corresponding subclass and connect each view to the File's Owner view outlet. Also size each view to account for the bar (you can use Simulated Metrics in the attribute inspector to do this).

To the @interface of the top view controller add an IBOutlet for the tab bar along with ivars for each of the new view controllers. Also add a view controller ivar named currentViewController which you can use to keep track of the currently selected controller:

@property (nonatomic, retain) IBOutlet UITabBar *myTabBar; // <-- connect to the tab bar in IB
@property (nonatomic, retain) UIViewController *tab1ViewController;
@property (nonatomic, retain) UIViewController *tab2ViewController;
@property (nonatomic, assign) UIViewController *currentViewController;

When you switch views, use insertSubview:belowSubview: to keep the tab bar visible. E.g. this code could be in a switch statement in tabBar:didSelectItem::

case 2:
if (tab2ViewController == nil) {
self.tab2ViewController =
[[Tab2ViewController alloc] initWithNibName:Tab2View bundle:nil];
}
[self.view insertSubview:tab2ViewController.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = tab2ViewController;
break;

The above should get you started in the right direction. Is there enough here so you can code the rest by yourself?

- Ray

Dec 8, 2009 1:38 PM in response to RayNewbie

Thank you very much raynewbie, you rock!

I posted complete example here. Use
svn co http://pymbian.svn.sourceforge.net/svnroot/pymbian/stuff/testtab_raynewbie/ to check it out.

It has following structure:

AppDelegate
- testtabViewController: UIViewController
-- tabviewtest: UIViewController <UITabBarDelegate> (this uses your sample code)
--- UITabBar
---- Item1: UITabBarItem
---- Item2: UITabBarItem

I also added that you specify the starting tab when tab bar is shown in first time. See http://pymbian.svn.sourceforge.net/svnroot/pymbian/stuff/testtab_raynewbie/Class es/tabviewtest.m

- (void)viewDidLoad {
[super viewDidLoad];
[myTabBar setSelectedItem:[myTabBar.items objectAtIndex:0]];
[self activateTab:1]; // this is same as your didSelectItem at post above
}

I hope this is not too kludge version, and keeps working on future version of iPhone SDK.

Message was edited by: ssalminen

Message was edited by: ssalminen

Dec 8, 2009 8:32 PM in response to ssalminen

Thanks so much for letting me know that code was useful to you!! It looks like you did a nice job of turning the outline into a complete sample app. Maybe the project could be titled "Tab Bar Controller Lite": For use when limited tab bar functionality is acceptable in exchange for the ability to place the controller below the root.

I hang out here primarily because I enjoy teaching (and of course learning a lot in the process!). I don't often know whether my instructions or sample code were useful to anyone besides the owner of the context. It's an honor to see what you built from my example at your site.

\- Ray

Apr 14, 2010 3:13 PM in response to RayNewbie

Hello Everyone,

This thread has been super helpful for coding a part of my application.
So basically, the entire application is navigation based.
The home screen is a table view with a list of sub applications with each cell loading the home screen view of a sub application.

I have used the examples above to create one of the sub application as a UIViewController, with a tab bar below the view. For each tab, I have a seperate class (UIViewController) that represents each tab's view. All of these is very similar to the example project code posted above.

Now, for one of the tab views it contains a search bar and a tableview. I have the searching and displaying of the results in the cell's of the tableview all working currently. The problem is, when I click on the cell, I want to be able to push the new view on top of the current view (search bar, table view) with the tab bar below. This is the method that I have implemented below which is called when clicking on a cell in the tableview. However, nothing changes when I click on the cell. I feel the problem is that I have to do something along the lines similar to the activatetab method implemented in the example.



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"Cell clicked!");
UIViewController *newView = \[\[tab1DetailView alloc] initWithNibName:@"tab1DetailView" bundle:nil];

\[self.navigationController pushViewController:newView animated:YES];

}


I will explain three following classes..

tab1ViewController //the view controller for the first tab
tab1DetailView //the detailed view for a search result.
CRMViewController //the sub app which is the sub application loading the main view with the tab bar, view and nav bar at the top.


I am not sure if this is clear enough but if someone could lead me in the right direction, that would be much appreciated! If something needs to described in a clearer fashion, I can do that as well. Thank you!

May 21, 2010 10:18 PM in response to bk23

Hi BK, and welcome to the Dev Forum!

For some reason I didn't see inbaebae's post last month so didn't ask some of the questions raised. Note that the example I posted was in response to a somewhat unusual requirement: The OP needed to retrofit a tab bar controller below the root level of a finished app. The example basically provides a "TabBarControllerLite" class, which, unlike the full version in the UIKit, doesn't need to be the root controller.

Instead of framing your question in terms of inbaebae's post, I would recommend opening your own thread with a title specific to your problem, a description of your view hierarchy, details of the problem, and the code of any method you think to be directly related. Please be sure to also include any warning or error messages printed in your Build Results and Debugger Console windows.

\- Ray

Aug 22, 2010 8:47 AM in response to MUsman

Thanks a ton guys! I've been trying solution after solution for this matter to find nothing at all, but a waste of coding time (but did learn a lot more as to what should be done for other matters). Regardless, this is a must know situation for any developer. For most people, using one type of template isn't going to cut it for their application, and knowing how to incorporate multiple types like so is crucial.

I was trying to use a tabbar to show web views, and settings, that I wanted outside of the main app, and not for app navigation. This worked perfect!! Thank you again!

Aug 23, 2010 7:02 AM in response to RayNewbie

Hello Guyz... I know this thread is quite old but iam really having headache trying to solve my problem. I have an application where when the application runs u come to a menue of a social networking application. however, i made a button to make a flip to another view. I wanted that view to have the control managing the UITabBar and thats why I added the UITabBar on the top of that Specific View. that view is called (SecondViewController) and i declared the UITabBar, and a two view Controllers (ActivityViewControllers, MapViewController). All i wanted to show is a view for each of the tabs built on the SecondViewController. I followed this thread step by step and i get a bunch of errors

error: expected expression before 'SecondViewController'
error: expected ':' before '.' token
confused by earlier errors, bailing out




- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
}




- (void)activateTab:(int)index {
switch (index) {
case 1:
if (SecondViewController.ActivityViewController == nil) {
self.ActivityViewController =
[[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil];
}
[self.view insertSubview:ActivityViewController.view belowSubview:rootTabBar];
if (SecondViewController.ActivityViewController != nil)
[SecondViewController.view removeFromSuperview];
SecondViewController = ActivityViewController;
break;
case 2:
if (WebViewController == nil) {
self.WebViewController =
[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
}
[self.view insertSubview:WebViewController.view belowSubview:rootTabBar];
if (SecondViewController != nil)
[SecondViewController.view removeFromSuperview];
SecondViewController = WebViewController;
break;
default:
break;
}
}





- (void)viewDidLoad {
[super viewDidLoad];
[rootTabBar setSelectedItem:[rootTabBar.items objectAtIndex:0]];
[self activateTab:1];



Any Help Would be really appreciated...

How to use TabBar in viewbase application

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.