Skip to content

Instantly share code, notes, and snippets.

@alexj70
Last active June 7, 2021 12:10
Show Gist options
  • Save alexj70/9814b431086e1e3deeb3f4c2976703f8 to your computer and use it in GitHub Desktop.
Save alexj70/9814b431086e1e3deeb3f4c2976703f8 to your computer and use it in GitHub Desktop.
UIStoryboard extension
import UIKit
extension UIStoryboard {
/// Main storyboard
public var main: UIStoryboard {
return UIStoryboard(name: "Main", bundle: nil)
}
/// Instantiates and returns the view controller with the specified identifier.
///
/// - Parameter identifier: uniquely identifies equals to Class name
/// - Returns: The view controller corresponding to the specified identifier string. If no view controller is associated with the string, this method throws an exception.
public func instantiateViewController<T>(withIdentifier identifier: T.Type) -> T where T: UIViewController {
let className = String(describing: identifier)
guard let vc = self.instantiateViewController(withIdentifier: className) as? T else {
fatalError("Cannot find controller with identifier \(className)")
}
return vc
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment