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