Instantiate and Present a viewController in Swift

Posted by Wei Xia on March 13, 2017 | Hits:


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


Swift 3

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


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

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


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


This is the end of post