Instantiate and Present a viewController in Swift

Posted by Wei on March 13, 2017

Question

How to instantiate and then presenting a viewController from a specific storyboard?

Answer

Swift 3

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil)

Issue

For people using this answer to instantiate UIViewController and are having the exception:

fatal error: use of unimplemented initializer ‘init(coder:)’ for class

Solution

Manually implement init(coder: NSCoder!) at your destination UIViewController that you are trying to instantiate.

init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}

If you need more description please refer to this answer here

Reference


This is the end of post