【发布时间】:2013-09-25 09:24:07
【问题描述】:
我的 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];
然后一切都完美加载。我做错了什么?
【问题讨论】:
-
试试这个,NSArray *multipleViewControllers=[[NSArray alloc] initWithObjects:[self ViewControllerA],[self ViewControllerB],[self ViewControllerC],nil]; [self.tabBarController setViewControllers:multipleViewControllers];
-
检查您是否在自定义控制器的
viewDidLoad或loadView或viewWillAppear方法中做错了,因为您的代码看起来很完美。 -
放置异常断点并检查它到底在哪里崩溃。
-
谢谢,这看起来是正确的 - 它在 viewDidLoad 方法中崩溃了。它崩溃的那一行是视图控制器的创建:“[[[self NavigationController] view] setFrame:CGRectMake(0,-20,ScreenWidth,ScreenHeight - (ControllerHeightOffset+29))];"不幸的是,这让我很困惑!
标签: ios cocoa-touch uiviewcontroller uitabbarcontroller