【问题标题】:How do I change "initwithNibName" in storyboard?如何更改情节提要中的“initwithNibName”?
【发布时间】:2012-02-09 13:19:39
【问题描述】:

我想用 Xcode 4.2 更改下面的代码。

UIViewController * example     = [[ExampleViewController alloc] initWithNibName:@"ExampleViewController" bundle:nil];

现在 ExampleViewController.xib 文件存在。 但我想用故事板来做。 请帮我。 (我英语不好。对不起)

【问题讨论】:

    标签: ios xcode storyboard xib


    【解决方案1】:

    UIStoryboard 类是你的朋友:

    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"mystoryboard"
                                                  bundle:nil];
    UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ExampleViewController"];
    

    【讨论】:

    • 谢谢~^^ 你的回答很有帮助。
    • 斯蒂芬,谢谢!我一直在寻找相当长的时间来试图弄清楚如何实例化 Storyboard 的视图。你的回答(和 jokor7 的问题)就像干涸的撒哈拉沙漠中的一杯新鲜水。
    • 确实是你的朋友。如果你只有一个主故事板,你不需要像上面那样初始化它,只需调用:[self.storyboard instantiateViewControllerWithIdentifier:@"<controller_identifier>"];
    • 做到了!非常适合从登录视图控制器切换到导航控制器。谢谢
    【解决方案2】:
    • 如果它仍在其自己的 xib 文件中,则无需更改任何内容。
    • 如果您已将所有内容移至情节提要中,则通常不需要这样做,因为您会使用 segue 在视图控制器之间进行链接。

    如果以上都不是真的,即你的视图控制器在故事板上但没有segue连接到它,那么你需要the documentation中描述的UIStoryboard的instantiateViewControllerWithIdentifier:方法。您必须在情节提要中设置标识符才能使其正常工作。

    【讨论】:

    • 感谢@jrturton ^^ 我的视图控制器在情节提要上,但没有继续。并将 initWithNibName 代码更改为 'UIViewController *example = [self.view instantiateViewControllerWithIdentifier:@"ExampleView"];'。但它有错误。两个代码不相等?
    • @jokor7 您不会将instantiateViewControllerWithIdentifier: 发送到 self.view 或任何其他视图,而是将其发送到情节提要。更具体地说,您需要将其发送到包含视图控制器的 故事板。请参阅 UIStoryboard 类。
    • @jokor7 真的无法为 Caleb 的评论添加更多内容。有了这个和斯蒂芬的回答,您应该拥有所需的一切。
    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多