【问题标题】:Crashing when adding custom view controllers to UITabBarController将自定义视图控制器添加到 UITabBarController 时崩溃
【发布时间】: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];
  • 检查您是否在自定义控制器的viewDidLoadloadViewviewWillAppear 方法中做错了,因为您的代码看起来很完美。
  • 放置异常断点并检查它到底在哪里崩溃。
  • 谢谢,这看起来是正确的 - 它在 viewDidLoad 方法中崩溃了。它崩溃的那一行是视图控制器的创建:“[[[self NavigationController] view] setFrame:CGRectMake(0,-20,ScreenWidth,ScreenHeight - (ControllerHeightOffset+29))];"不幸的是,这让我很困惑!

标签: ios cocoa-touch uiviewcontroller uitabbarcontroller


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-01-10
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多