I have an app with a Tabbar controller containing three tabs. I need to add a navigation controller to the first tab. My root View controller is the tabbar so how to I add the navigation bar? I have the navigation bar initialized but dont know where to set it. Thanks to anyone that can help and ask if you need more information. This is my app delegate.m:
#import "TACAppDelegate.h"
#import "FirstViewController.h"
#import "ThirdViewController.h"
#import "SecondViewController.h"
@implementation TACAppDelegate
@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* Initialize window view */
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
/* Initialize tab bar controller, add tabs controllers */
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [self initializeTabBarItems];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return (YES);
}
....
- (NSArray *)initializeTabBarItems
{
NSArray * retval;
/* Initialize view controllers */
FirstViewController *viewController1 = [[FirstViewController alloc] init];
SecondViewController *viewController2 = [[SecondViewController alloc] init];
ThirdViewController *viewController3 = [[ThirdViewController alloc] init];
/* Initialize navigation controllers */
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,nil];
return (retval);
}
@end
This is the line that needs to change to do this in code:
Here, you are still putting a reference to the original viewController1 into the array of tab bar items. That’s no longer what you want. You’ve created the navigation controller already and set its root view controller to your view controller 1, so that’s the top-level reference you need to put in the list of tab bar items. Your new line would look like this:
This would create a controller hierarchy like:
TabBar:[ NavController:[ VC1 ], VC2, VC3 ]