Loading specific ViewControllers on tabBarController.

Discussion in 'iOS Programming' started by RottenApple2, Oct 12, 2011.

  1. RottenApple2 macrumors newbie

    Nov 4, 2009
    Hi guys, I am having problems loading specific ViewControllers on the tabBarController. This is what my project looks like:

    #import "TabAppDelegate.h"
    #import "ViewControllerA.h"
    #import "ViewControllerB.h"
    //etc #import up to F 
    - (void)applicationDidFinishLaunching:(UIApplication *)application 
        tabBarController = [[UITabBarController alloc] init];
        UIViewController *viewController1 = [[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil] autorelease];
        UIViewController *viewController2 = [[[ViewControllerA alloc] initWithNibName:@"ViewControllerB" bundle:nil] autorelease];
        tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
        [window addSubview:tabBarController.view];
        [self.window makeKeyAndVisible];
        return YES;
    Interface builder
    Tab Bar Controller

    Each ViewController is linked to a class bearing the same name.

    As you can see from the diagram above, in Interface Builder I've got a Tab Bar Controller inside which there are lots of View Controllers. In applicationDidFinishLaunching I have written some (clearly wrong!) code with the purpose of only displaying controller A and B within the Tab Controller at launch. However the program crashes immediately with the following error message: "Thread 1: Program received signal: "SIGABRT"".

    If in applicationDidFinishLaunching I omit the first 4 lines of code (i.e. I don't try to load ONLY ViewControllerA and ViewControllerB) the program launches perfectly, but with ALL the ViewControllers…

    Any idea on what I am doing wrong??
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    First, I think you're leaking tabBarController.

    Also, what happens if you try:
    self.window.rootViewController = tabBarController;
    instead of:
    [window addSubview:tabBarController.view];
  3. RottenApple2 thread starter macrumors newbie

    Nov 4, 2009
    It still crashes in the same way..
    How can I sort the leak of tabBarController?
  4. North Bronson macrumors 6502

    Oct 31, 2007
    San José
    If you add the view controller's view directly to your window, you still need to keep a reference to the view controller with an instance variable; the window will retain the view, but will not retain the controller.

    If you assign a view controller to be the root view controller of the window, the window will retain the controller for you so that you do not have to keep an instance variable of the controller.

    The code looks solid. You might be trying to access something bad in the actual NIB file itself.
  5. RottenApple2 thread starter macrumors newbie

    Nov 4, 2009
    Thanks for the explanations on the leakage.

    I agree with you that the problem must lie in some illicit connections I made in Interface Builder. I don't know how to solve it though..
  6. Sykte macrumors regular

    Aug 26, 2010
    Look at the view controller class you're alloc/initing compared to the nib you're loading.

Share This Page