paulsamuels / SBConstants

MIT License
311 stars 28 forks source link

Convienience methods to utilize constants #55

Open multinerd opened 6 years ago

multinerd commented 6 years ago

The code below is based on Reusable and uses the StoryboardNames generated to init VCs

Storyboard Methods ```swift extension UIStoryboard { /// Convenience Initializers convenience init(storyboard: StoryboardNames, bundle: Bundle? = nil) { self.init(name: storyboard.rawValue, bundle: bundle) } /// Class Functions public static func storyboard(storyboard: StoryboardNames = .Main, bundle: Bundle? = nil) -> UIStoryboard { return UIStoryboard(name: storyboard.rawValue, bundle: bundle) } /// View Controller Instantiation from Generics func instantiateViewController(_: T.Type) -> T { guard let viewController = self.instantiateViewController(withIdentifier: T.storyboardIdentifier) as? T else { fatalError("Couldn't instantiate view controller with identifier \(T.storyboardIdentifier) ") } return viewController } } ```

Usage: let vc = UIStoryboard(storyboard: .Main)

ViewController Methods ```swift extension UIViewController { public static func instantiateFromStoryboard(name storyboard: StoryboardNames = .Main) -> Self { func instantiateFromStoryboard(_: T.Type) -> T { let sb = UIStoryboard(storyboard: storyboard) return sb.instantiateViewController(withIdentifier: String(describing: T.self)) as! T } return instantiateFromStoryboard(self) } } ```

Usage: let vc = SearchViewController.instantiateFromStoryboard(name: .Main)

I'm new to ruby or I'd make the PR myself, leaving the idea here if anyone does want to implement it.