ios - Objective-C instantiateViewControllerWithIdentifier 返回 nil

标签 ios objective-c uiviewcontroller

一周后我打开了我的项目,似乎对于我在 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。

enter image description here

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

似乎无法弄清楚问题是什么。感谢您的帮助。

我有一个抽屉导航。切换到其他 ViewController,除了带有标识符 'GchConnect' 的那个(即 indexPath.section == 1 和 indexpath.row == 0 )是有效的。它们都在同一个 Storyboard中

最佳答案

如果你有多个 Storyboard,self.storyboard 返回当前 UIViewController 所在的 Storyboard。你需要检查 Storyboard的名称是否为“GchConnect”。并用 storyboardWithName:bundle: 实例化该 Storyboard

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

关于ios - Objective-C instantiateViewControllerWithIdentifier 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545467/

相关文章:

ios - 在横向模式下使用 Core Motion

ios - 移除 KVO 观察器时 APP 崩溃

ios - Refresh Control with Search Display Controller 出现在搜索栏上方

iOS:在当前模态视图 Controller 后面切换 View Controller ?

ios - 将数据从 Objective-C Viewcontroller 传递到 Swift ViewController

ios - 解析 - xcode 中的多条件查询问题( objective-c )

objective-c - Xcode 中的 EXC_BAD_ACCESS (code=1) 错误

ruby-on-rails - 使用 Phonegap 作为 Rails 3 应用程序的本地容器

ios - 如何在 webViewDidFinishLoad 中运行一次命令?

ios - 启动时自定义 View Controller