Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 444
kender Avatar asked Nov 13 '11 11:11

kender


People also ask

How do I add a ViewController to a storyboard?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.

How do you create an entry point on a storyboard?

Click on Top of ViewController or Controller in Document Outline window (on left side) of the Controller you want to make Storyboard Entry Point. Click on Attribute inspector button (in right side). Enable "Is Initial View Controller".

How do I change the initial view controller in storyboard?

Specifying the Initial View Controllerstoryboard and select the Tab Bar Controller Scene. On the right, select the Attribute inspector. You'll find a checkbox named Is Initial View Controller. Checking this box will identify the selected view controller as the initial entry point for the storyboard you're on.


2 Answers

For Swift 4

let viewController = UIStoryboard.init(name: "MainStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ResultsController")
self.present(viewController, animated: true, completion: nil)
like image 63
Kiryl Belasheuski Avatar answered Sep 27 '22 22:09

Kiryl Belasheuski


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];
like image 34
mafis Avatar answered Sep 27 '22 22:09

mafis