cjuzng
Last Updated: August 05, 2018
·
80.51K
· anthonylevings
Speechrocket4 square

Swift: Instantiate a view controller using its Storyboard name in Xcode

First of all give the view controller in your storyboard a Storyboard ID inside the identity inspector. Then use the following example code (ensuring the class, storyboard name and story board ID match those that you are using):

let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
// .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it

self.presentViewController(viewController, animated: false, completion: nil)

For more details see http://sketchytech.blogspot.com/2012/11/instantiate-view-controller-using.html

Say Thanks
Respond

2 Responses
Add your response

29956

hello,

Xcode 8 when it compiles says to change instantiate viewcontroller with identifier to simply, instantiate view controller. I did that, why does it give two errors?

I'm working with Swift 3. I want to change pages programmatically.I've read a lot of other questions on the topic. All of them use instantiate view controller with the identifier. They haven't adopted the new language.
regards
https://www.ucbrowser.vip/ https://shareit.onl/ https://mxplayer.pro/

3 months ago ·
30295
13 days ago ·