How to create a UIViewController layout in storyboard and then use it in code?

天大地大妈咪最大 提交于 2019-11-27 21:18:56

问题


I have a Storyboard in my iOS 5 application.

In there I have created a number of screens and it works perfectly.

However there's one view controller that I create in code, not as a result of UI action but at the end of processing data. I would like to show this view controller then, as a modalViewController, but also have it designed in the storyboard editor.

Is it possible? Using the nibs I did it like this:

ResultsController *rc = [[ResultsController alloc] initWithNibName:@"ResultsController"
                                                            bundle:nil];
[self.navigationController presentModalViewController:rc animated:YES];
[rc release];

Right now I don't really have a nib files, so how do I do it?


回答1:


Take a look at the UIStoryboard class. There is a instantiateViewControllerWithIdentifier Method. So you need to set the Identfier within the Storyboard Editor for your ResultsController ViewController.

You can do something like this

UIViewController *viewController = 
   [[UIStoryboard storyboardWithName:@"MainStoryboard" 
                              bundle:NULL] instantiateViewControllerWithIdentifier:@"ResultsController"];

[self presentModalViewController:viewController animated:NO];



回答2:


In your storyboard:

  1. Add a generic UIViewController.
  2. With the Identity Inspector, set its custom class as your ResultsController.
  3. Create a modal segue from your source view controller to the ResultsController



回答3:


For Swift 4

let viewController = UIStoryboard.init(name: "MainStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ResultsController")
self.present(viewController, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/8111218/how-to-create-a-uiviewcontroller-layout-in-storyboard-and-then-use-it-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!