ios 如何在情节提要中更改“initwithNibName”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8848857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:16:19  来源:igfitidea点击:

How do I change "initwithNibName" in storyboard?

iosxcodestoryboardxib

提问by jokor7

I want to change below code with storyboard with Xcode 4.2.

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

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

Now ExampleViewController.xib file exist. but I want to make it with storyboard. please help me. (I'm not good at English. Sorry)

现在 ExampleViewController.xib 文件存在。但我想用故事板制作它。请帮我。(我英语不好。抱歉)

回答by Stephen Darlington

The UIStoryboardclass is your friend:

UIStoryboard班是你的朋友:

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

回答by jrturton

  • If it is still in its own xib file, then you don't change anything.
  • If you've moved everything into a storyboard, then you wouldn't often need to do this as you'd link between view controllers using segues.
  • 如果它仍然在它自己的 xib 文件中,那么你不需要改变任何东西。
  • 如果您已将所有内容移动到故事板中,那么您通常不需要这样做,因为您将使用 segue 在视图控制器之间进行链接。

If neither of the above are true, i.e. your view controller is on the storyboard but no segue connects to it, then you want UIStoryboard's instantiateViewControllerWithIdentifier:method described in the documentation. You have to set the identifier in the storyboard for this to work.

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