Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RottenApple2

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


Code:
TabAppDelegate.m
--------------------

#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
ViewControllerA
ViewControllerB
ViewControllerC
ViewControllerD
ViewControllerE
ViewControllerF

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??
Thanks!
 
First, I think you're leaking tabBarController.

Also, what happens if you try:
Code:
self.window.rootViewController = tabBarController;
instead of:
Code:
[window addSubview:tabBarController.view];
 
It still crashes in the same way..
How can I sort the leak of tabBarController?
Cheers.
 
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.
 
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..
 
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..

Look at the view controller class you're alloc/initing compared to the nib you're loading.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.