Ios 将自定义视图控制器添加到UITabBarController时崩溃
我的iOS 6应用程序最近在向UITabBarController添加自定义视图控制器时开始崩溃 我的AppDelegate具有公共属性Ios 将自定义视图控制器添加到UITabBarController时崩溃,ios,cocoa-touch,uiviewcontroller,uitabbarcontroller,Ios,Cocoa Touch,Uiviewcontroller,Uitabbarcontroller,我的iOS 6应用程序最近在向UITabBarController添加自定义视图控制器时开始崩溃 我的AppDelegate具有公共属性 @property (readwrite, strong) UITabBarController *TabBarController; @property (readwrite, strong) ViewControllerTypeA *ViewControllerA; //extends UIViewController Class @property (
@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”而终止应用程序,原因:'-[\u NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象[1]插入nil对象'
但是,如果我改为插入默认UIViewController类,即:
[[self TabBarController] setViewControllers:@[[[UIViewController alloc] init],[[UIViewController alloc] init],[[UIViewController alloc] init]] animated:NO];
然后所有的东西都能完美地装载。我做错了什么?问题可能是您试图将一个nil对象插入字典,因为这里没有字典,它可能是第一个视图控制器循环中的某个东西。哇,最后它与在UINavigationBar中使用自定义字体有关。很明显,它试图在设置字体之前初始化NavController,所以当我试图访问视图时,它就崩溃了。疯子谢谢大家的帮助 试试这个,NSArray*MultipleViewController=[[NSArray alloc]initWithObjects:[self-ViewControllerA],[self-ViewControllerB],[self-ViewControllerC],nil];[self.tabBarController setViewControllers:multipleViewControllers];检查您在自定义控制器的
viewDidLoad
或loadView
或viewwillbeen
方法中是否有错误,因为您的代码看起来很完美。请放置异常断点并检查其崩溃的确切位置。谢谢,这看起来是正确的-它在viewDidLoad方法中崩溃。它崩溃的原因是创建了一个视图控制器:“[[[self-NavigationController]view]setFrame:CGRectMake(0,-20,屏幕宽度,屏幕高度-(ControllerHeightofSet+29))”;“不幸的是,这同样让我感到困惑!