【问题标题】:Could not load NIB in bundle with name 'SecondViewController'无法在名称为“SecondViewController”的捆绑包中加载 NIB
【发布时间】:2012-09-08 17:34:31
【问题描述】:

我正在尝试将 Navigator 控制器推送到不同的视图控制器,但我不断收到错误消息,提示无法加载 nib 包名称。这是我正在使用的代码:

SecondViewController *vc1 = [[SecondViewController alloc]
      initWithNibName:NSStringFromClass([SecondViewController class]) bundle:Nil];
[self.navigationController pushViewController:vc1 animated:YES];

【问题讨论】:

    标签: objective-c xcode pushviewcontroller


    【解决方案1】:

    您必须传递 nib 文件的名称,而不是类名:

    // Assuming there is a properly set up SecondViewController.xib in your project.
    SecondViewController *vc1 = [[SecondViewController alloc] 
                                initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController pushViewController:vc1 animated:YES];
    

    编辑:

    如果您使用 Storyboard,则必须以不同的方式加载您的 GUI:

    UIStoryboard *sboard = [UIStoryboard storyboardWithName:@"StoryboardFileName" 
                                                     bundle:nil];
    SecondViewController *vc1 = [sboard instantiateInitialViewController];
    

    【讨论】:

    • 我试过这样做,但我仍然得到错误。这是整个错误:2012-09-15 20:07:45.127 Passdata.app[4010:907] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在包中加载 NIB:'NSBundle var/mobile/Applications/2BFE10F0-1513-44FE-AD99-7E928C739E9C/Passdata.app> (已加载)' 名称为 'SecondViewController'' *** 第一次抛出调用堆栈:
    • 您的项目中是否有 SecondViewController.xib 并添加到您的目标中?
    • 我正在使用情节提要,所以我没有 xib 文件,这会导致问题吗?
    • 解决了这个问题,但我也将一个字符串传递给 SecondViewController 所以我现在收到这个错误,你知道吗?:2012-09-15 20:28:10.942 Passdata.app[4083:907] -[UINavigationController setTmpString:]:无法识别的选择器发送到实例 0x1d57f530 2012-09-15 20:28:10.946 Passdata.app[4083:907] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序', 原因:'-[UINavigationController setTmpString:]: unrecognized selector sent
    • @StørmShadøws,错误是说你正试图在 UINavigationController 上调用 setTmpString -- 你应该在 SecondViewController 上调用它
    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多