Question

Hi I am new to iOS and I am trying to display a Navigation Controller in a TabBar based app.

Here is is what I got: H file:

#import <UIKit/UIKit.h>



@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UITabBarController *tabBarController;

@property (strong, nonatomic) UINavigationController *navigationController;

@end

M file:

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
    UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3,viewController4,viewController5];



    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController5];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;

}
Was it helpful?

Solution

Instead of adding viewController5 to the tab bar controller's view controllers,, add the navigation controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
    UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];

    self.tabBarController = [[UITabBarController alloc] init];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController5];
    self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3,viewController4,self.navigationController];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;

}

OTHER TIPS

This doesn't work. Mixing the view controllers with the navigation controller gives you view controllers without nav and a nav controller without a view controller. You need a nav controller for each vc and then create an array of those nav controllers.

Here is an answer that works: How to add UITabBarController programmatically (no xib file or storyboard)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top