如何在故事板中更改"initwithNibName"?

jok*_*or7 55 xcode storyboard xib ios

我想用Xcode 4.2改变故事板下面的代码.

UIViewController * example     = [[ExampleViewController alloc] initWithNibName:@"ExampleViewController" bundle:nil];
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*ton 132

UIStoryboard堂课是你的朋友:

UIStoryboard* sb = [UIStoryboard storyboardWithName:@"mystoryboard"
                                              bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ExampleViewController"];
Run Code Online (Sandbox Code Playgroud)

  • 这确实是你的朋友.如果你只有一个主要的故事板,你不需要像上面那样初始化它,但可以调用:`[self.storyboard instantiateViewControllerWithIdentifier:@"<controller_identifier>"];` (5认同)

jrt*_*ton 8

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

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