繁体   English   中英

Objective-C instantiateViewControllerWithIdentifier 返回 nil

[英]Objective-C instantiateViewControllerWithIdentifier returns nil

一周后我打开了我的项目,对于我在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' 的那个(即 indexPath.section == 1 和 indexpath.row == 0 )有效。 他们都在同一个故事板中

如果您有多个故事板,则self.storyboard返回当前UIViewController所在的那个。您需要检查 storybaord 的名称以获取“GchConnect”。 并使用storyboardWithName:bundle实例化该故事板:

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

使用此代码

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

试试这个代码,

如果您使用下面的代码显示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];

希望它有帮助

而不是

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)
}

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

我的问题是我添加了本地化,我添加到故事板的更改正在更新到模拟器/设备。

它通过以下方式修复它:

选择故事板,然后

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM