将自定义视图控制器添加到UITabBarController时崩溃

时间:2013-09-20 05:03:34

标签: ios cocoa-touch uiviewcontroller uitabbarcontroller

我的iOS 6应用程序最近在向UITabBarController添加自定义视图控制器时崩溃了。

我的AppDelegate具有公共属性

@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

最初,我创建一个标签栏控制器,并将视图控制器添加到此:

- (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];

}

它在最后一行崩溃并出现错误:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象插入nil对象[1]'

但是,如果我改为插入默认的UIViewController类,即:

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

然后一切都完美地加载。我做错了什么?

2 个答案:

答案 0 :(得分:0)

问题可能是你试图将nil对象插入到字典中,因为这里没有字典,它可能是第一个视图控制器循环内的东西。

答案 1 :(得分:0)

哇,最后它与在UINavigationBar中使用自定义字体有关。显然它是在设置字体之前尝试初始化NavController - 所以当我试图访问视图时它就崩溃了。疯。谢谢大家的帮助!