我的 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:]:尝试从对象 [1] 插入 nil 对象”
但是,如果我改为插入默认 UIViewController 类,即:
[[self TabBarController] setViewControllers:@[[[UIViewController alloc] init],[[UIViewController alloc] init],[[UIViewController alloc] init]] animated:NO];
然后一切都完美加载。我究竟做错了什么?