I am having problems loading specific ViewControllers on the tabBarController. This is what my project looks like:
T开发者_Python百科abAppDelegate.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!
tabBarController = [[UITabBarController alloc] init];
isn't needed if its already linked to your UITabBarController
in the Interface Builder:
Try this:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
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;
}
精彩评论