Created
February 24, 2017 15:37
-
-
Save nakiwo/cf03ad4b0b74a55fdc9295dce812e6e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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