如何在故事板中更改“initwithNibName”?

时间:2012-01-13 10:10:43

标签: ios xcode storyboard xib

我想使用Xcode 4.2在故事板中更改以下代码。

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

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

2 个答案:

答案 0 :(得分:132)

UIStoryboard班是你的朋友:

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

答案 1 :(得分:8)

  • 如果它仍然在自己的xib文件中,那么您不会更改任何内容。
  • 如果您已将所有内容都移动到故事板中,那么您通常不需要这样做,因为您使用segues在视图控制器之间进行链接。

如果上述两种情况均属实,即您的视图控制器位于故事板上但没有segue连接到它,那么您需要the documentation中描述的UIStoryboard的instantiateViewControllerWithIdentifier:方法。您必须在故事板中设置标识符才能使其生效。

相关问题