Skip to content

Instantly share code, notes, and snippets.

@nakiwo
Created February 24, 2017 15:37
Show Gist options
  • Save nakiwo/cf03ad4b0b74a55fdc9295dce812e6e7 to your computer and use it in GitHub Desktop.
Save nakiwo/cf03ad4b0b74a55fdc9295dce812e6e7 to your computer and use it in GitHub Desktop.
// Swift 3
import UIKit
class TestViewController: UIViewController {
var text: String!
static func create(with text: String) -> Self {
let vc = instantiateFromStoryboard()
vc.text = text
return vc
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension UIViewController {
static func instantiateFromStoryboard() -> Self {
return instantiateFromStoryboardHelper()
}
private static func instantiateFromStoryboardHelper<T>() -> T {
let name = String(describing: self)
let storyboard = UIStoryboard(name: name, bundle: nil)
guard let controller = storyboard.instantiateInitialViewController() as? T else {
preconditionFailure("'\(name)': instantiateInitialViewController failed.")
}
return controller
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment