0
votes

My iOS 6 application recently started crashing when adding custom view controllers to a UITabBarController.

My AppDelegate has public properties

@property (readwrite, strong) UITabBarController *TabBarController;
@property (readwrite, strong) ViewControllerTypeA *ViewControllerA;  //extends UIViewController Class
@property (readwrite, strong) ViewControllerTypeB *ViewControllerB;  //extends UIViewController Class
@property (readwrite, strong) ViewControllerTypeC *ViewControllerC;  //extends UIViewController Class

Initially, I create a tab bar controller, and add the view controllers to this:

- (void)InitializeTabBar {

[self setTabBarController:[[UITabBarController alloc] init]];
UITabBarItem *Tab1 = [[UITabBarItem alloc] init];
UITabBarItem *Tab1 = [[UITabBarItem alloc] init];
UITabBarItem *Tab2 = [[UITabBarItem alloc] init];

[self setViewControllerA:[[ViewControllerTypeA alloc] init]];
[self setViewControllerB:[[ViewControllerTypeB alloc] init]];
[self setViewControllerC:[[ViewControllerTypeC alloc] init]];

[[self ViewControllerA] setTabBarItem:Tab1];
[[self ViewControllerB] setTabBarItem:Tab2];
[[self ViewControllerC] setTabBarItem:Tab3];

[[self TabBarController] setViewControllers:@[[self ViewControllerA],[self ViewControllerB],[self ViewControllerC]] animated:NO];

}

And it crashes on the last line with error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

However, if I instead insert default UIViewController Classes, i.e.:

[[self TabBarController] setViewControllers:@[[[UIViewController alloc] init],[[UIViewController alloc] init],[[UIViewController alloc] init]] animated:NO];

Everything then loads perfectly. What am I doing wrong?

2
Try this, NSArray *multipleViewControllers=[[NSArray alloc] initWithObjects:[self ViewControllerA],[self ViewControllerB],[self ViewControllerC],nil]; [self.tabBarController setViewControllers:multipleViewControllers];karthika
check whether you are doing anything wrong in viewDidLoad or loadView or viewWillAppear method of your custom controller, because your code looks perfect.Prasad Devadiga
Put the exceptional break point and check where exactly it is crashing.Prasad Devadiga
Thanks, this appears correct - it is crashing in the viewDidLoad method. The line on which it crashing is the creation of a view controller: "[[[self NavigationController] view] setFrame:CGRectMake(0,-20,ScreenWidth,ScreenHeight - (ControllerHeightOffset+29))];" Unfortunately this confuses me just as much!Stuart Barth

2 Answers

0
votes

The problem probably is that you are trying to insert a nil object into a dictionary, since there are no dictionary around here, it could be something inside the cycle of the first view controller.

0
votes

Wow, in the end it had to do with using a custom font in the UINavigationBar. Apparently it was trying to initialize the NavController before the font was set - so it crashed as soon as I tried to access the view. Crazy. Thanks for the help all!