Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AppStoryboard enumeration
//
// AppStoryboards.swift
// AppStoryboards
//
// Created by Gurdeep on 15/12/16.
// Copyright © 2016 Gurdeep. All rights reserved.
//
import Foundation
import UIKit
enum AppStoryboard : String {
case Main
var instance : UIStoryboard {
return UIStoryboard(name: self.rawValue, bundle: Bundle.main)
}
func viewController<T : UIViewController>(viewControllerClass : T.Type, function : String = #function, line : Int = #line, file : String = #file) -> T {
let storyboardID = (viewControllerClass as UIViewController.Type).storyboardID
guard let scene = instance.instantiateViewController(withIdentifier: storyboardID) as? T else {
fatalError("ViewController with identifier \(storyboardID), not found in \(self.rawValue) Storyboard.\nFile : \(file) \nLine Number : \(line) \nFunction : \(function)")
}
return scene
}
func initialViewController() -> UIViewController? {
return instance.instantiateInitialViewController()
}
}
extension UIViewController {
// Not using static as it wont be possible to override to provide custom storyboardID then
class var storyboardID : String {
return "\(self)"
}
static func instantiate(fromAppStoryboard appStoryboard: AppStoryboard) -> Self {
return appStoryboard.viewController(viewControllerClass: self)
}
}
@Gurdeep0602

This comment has been minimized.

Copy link
Owner Author

commented Oct 30, 2016

Usage :

    let greenScene = GreenVC.instantiate(fromAppStoryboard: .Main)

    let greenScene = AppStoryboard.Main.viewController(viewControllerClass: GreenVC.self)

    let greenScene = AppStoryboard.Main.instance.instantiateViewController(withIdentifier: GreenVC.storyboardID)
@DivineDominion

This comment has been minimized.

Copy link

commented Jan 29, 2017

The usage example looks sweet! Thanks for sharing 👍

@vinamelody

This comment has been minimized.

Copy link

commented Feb 16, 2017

Hi, what are the file, line and function variables referring to?

@dtvanh

This comment has been minimized.

Copy link

commented May 31, 2017

Thanks for sharing. Keep it up bro ^^

@sreeram780

This comment has been minimized.

Copy link

commented Jun 8, 2017

Thanks for Sharing but root window was not setting properly.whose view is not in the window hierarchy getting this when i switch storyboards alternatively.

@wiamasosogt

This comment has been minimized.

Copy link

commented Jul 25, 2017

I like your architecture very much. I implement it right now with "CleanStore" project by Raymond Law. It works very fine as long as I build and run it. But I have an issue with TTD. Running the test raises an error in AppStoryBoards in the guard statement on line 26. I think the reason is that as long as the UIViewController.Type 's module (here "CleanStore.AnyVC" is the app itself there' s no problem. But starting the test, the module is the test module (here "CleanStoreTest.AnyVC"). "StoryBoard ID" 's are set correctly. Can you help please?

@vinothvino42

This comment has been minimized.

Copy link

commented Feb 6, 2019

Thank you so much man! Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.