Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load UIViewController programmatically from storyboard?

I created a UIViewController in my Main.storyboard with a few buttons and labels. I'm trying to switch to that view controller using self.presentViewController but it will not load the view from storyboard. It will only load a blank black screen by default. Any idea on how to load the view from what i've created in storyboard?

self.presentViewController(ResultViewController(), animated: true, completion: nil)
like image 461
ThatHybrid Avatar asked Aug 24 '14 16:08

ThatHybrid


People also ask

How do I load a view controller from storyboard?

In your storyboard go to the Attributes inspector and set the view controller's Identifier. You can then present that view controller using the following code. [sb instantiateInitialViewController] is handy if you want to start on the scene's default view controller. James, thank you!

How do I call a storyboard in Swift?

You need to give your viewcontroller an identifier. You do that by highlighting your view controller and then give it a identifier: You then put your identifier in StoryBoard ID. Yes!


1 Answers

The way you're doing this just creates a new instance of your view controller. It does not create one from the prototype you've defined in Interface Builder. Instead, you should be using this, where "SomeID" is a storyboard ID that you've assigned to your view controller in Interface Builder.

if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SomeID") as? ResultViewController {
    presentViewController(resultController, animated: true, completion: nil)
}

You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.

enter image description here

like image 124
Mick MacCallum Avatar answered Oct 06 '22 05:10

Mick MacCallum