【问题标题】:Objective-C instantiateViewControllerWithIdentifier returns nilObjective-C instantiateViewControllerWithIdentifier 返回 nil
【发布时间】:2020-02-08 13:28:02
【问题描述】:

一周后我打开了我的项目,似乎对于我在StoryBoard 中创建的所有新UIViewControllerinstantiateViewControllerWithIdentifier 的返回值为零。已经在项目中的所有ViewControllers 工作正常。

GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
navigationController.viewControllers = @[gchCVC];

第一行返回nil,我最初的想法是self.storyboard返回nil,所以我尝试了这个并设置了断点。

if (self.storyboard != nil) {
    GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    navigationController.viewControllers = @[gchCVC];
}

它进入 if 并在第二行崩溃..

我还正确添加了类和storyboard id。

如果我尝试加载另一个已经存在的ViewController,它可以工作,但不是新的。

似乎无法弄清楚问题所在。任何帮助表示赞赏。

我有一个导航抽屉。切换到其他 ViewController 除了带有标识符“GchConnect”的 ViewController(即 indexPath.section == 1 和 indexpath.row == 0 )有效。他们都在同一个故事板中

【问题讨论】:

  • 您需要将一个视图推送到另一个视图控制器吗?
  • 从使用故事板 id 中删除复选标记,然后使用 [UIStoryboard storyboardWithName:@"Main" bundle:nil] 获取参考故事板
  • 您是否无意中本地化了您的故事板,并仅添加了一个本地化版本的新视图控制器,而这不是您正在测试的当前版本?
  • 取消选中情节提要 ID,然后尝试。它可能工作!而navigationController.viewControllers 语句将viewControllers 设置为导航控制器,通常用于初始化导航控制器。你应该把 viewcontroller 推到导航控制器上
  • Module文本框内点击并按回车键。它可能会解决您的问题。

标签: ios objective-c uiviewcontroller


【解决方案1】:

如果您有多个情节提要,self.storyboard 返回当前UIViewController 所在的情节提要。您需要检查情节提要的名称是否为“GchConnect”。并使用storyboardWithName:bundle: 实例化该故事板

UIStoryboard storyboard = [UIStoryboard storyboardWithName:bundle:nil];
GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];

【讨论】:

  • @HeisenBerg 如果您只使用一个情节提要。然后你的代码对我来说是正确的。有时界面生成器是错误的。如果您能够使用较小的项目设置来重现它。我会推荐提交给bugreport.apple.com看看。
  • @HeisenBerg 和其他建议是:(1)确保 Xcode 是最新的; (2) 如果您还没有这样做,请重新启动您的 Xcode; (3) 确保您的 id 末尾没有提取空格; ...
【解决方案2】:

使用此代码

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
HomepageViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"GchConnect"];
[self.navigationController pushViewController:vc animated:YES];

【讨论】:

  • 这段代码的结果是什么?我的意思是黑屏?
  • 崩溃日志是什么?
  • NSScanner: 无字符串参数
  • 删除您的代码并为每种情况使用我的代码,同时导入该视图控制器
  • 它在第二行崩溃,没有进入我们 pushViewController 的第三行
【解决方案3】:

而不是

if (self.storyboard != nil) {
    GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    navigationController.viewControllers = @[gchCVC];
}

尝试使用:

if (self.storyboard != nil) {
    GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    navigationController.pushViewController(gchCVC, animated: true)
}

您应该做的是推送或呈现视图控制器。目前您正在尝试替换导航控制器中的所有视图控制器,

【讨论】:

    【解决方案4】:

    试试这个代码,

    如果您提供viewcontroller,请使用以下代码,

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                    @"YourStoryBoardName" bundle:[NSBundle mainBundle]];
    
    GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    
    [self presentViewController:gchCVC animated:YES completion:nil];
    

    或者如果您推送viewcontroller,请使用以下代码,

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                        @"YourStoryBoardName" bundle:[NSBundle mainBundle]];
    
    GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    
    [self.navigationController pushViewController:gchCVC animated:YES];
    

    希望对你有帮助

    【讨论】:

      【解决方案5】:

      我的问题是我添加了本地化并且我添加到情节提要的更改正在更新到模拟器/设备。

      它通过以下方式修复它:

      选择故事板,然后

      编辑器 > 本地化锁定 > 重置锁定控件

      【讨论】:

        【解决方案6】:

        确保 ViewController 的代码模块已添加到您正在构建的目标中。这为我解决了问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多