Use this shortcut to instantiate ViewController in Swift

Chanakya Hirpara
3 min readSep 1, 2021

Most of the times writing code for instantiating UIViewController we are doing like follow

let storyBoardRef = UIStoryboard(name: "StoryboardName", bundle: Bundle.main)let viewController = storyBoardRef.instantiateViewController(withIdentifier: "ViewController Identifier specified in Storyboard") as! CustomViewController

So to get rid of this issue of writing same and same code with different ViewController type if found amazing way to instantiate UIViewController using generics so that we can eliminate lots of code writing while having cup of coffee!

Prerequisite

  1. You must be using Storyboard
  2. Your UIViewController Storyboard ID should be same as your UIViewController class name. e.g lets say your UIViewController class name is “LoginVC” then the Storyboard ID of that ViewController should be also “LoginVC” as shown in below screenshot.

Now please follow the steps as given below

  1. Create new empty Swift file by File -> New -> File -> Select Swift File under Source -> Click Next by entering file name. e.g. “AppStoryboard”
  2. Create one enum inside your AppStoryboard.swift file with name. e.g “AppStoryboard”. as shown below.
enum AppStoryboard : String {}

3. Now we have to specify the storyboards used in our project. If its small project most probably we use only Main.storyboard that comes with project setup itself. But if the project is large and divided into multiple storyboards we can specify multiple storyboards as well inside our enum. For example lets say we are using storyboards namely Main.storyboard and PreLogin.storyboard, in such a case our enum will look like following

enum AppStoryboard : String{  case Main = "Main"  case PreLogin = "PreLogin"}

4. Now we have to create one computed property(which don’t actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly) namely instance that will return instance of the Storyboard based on name of the Storyboard inside our AppStoryboard enum. In our case either Main Storyboard or PreLogin Storyboard.

enum AppStoryboard : String{  case Main = "Main"  case PreLogin = "PreLogin"  var instance : UIStoryboard {    return UIStoryboard(name: self.rawValue, bundle: Bundle.main)  }}

5. So from now if we want instance of Main Storyboard or PreLogin Storyboard we can get it by

let mainStoryBoardInstance = AppStoryboard.Mainlet preLoginStoryBoardInstance = AppStoryboard.PreLogin

6. Now we want the UIViewController from storyboard instance, for that we can create one function in which we will have to pass class of the UIViewController and based on that the function will return the appropriate UIViewController.

enum AppStoryboard : String{  case Main = "Main"  case PreLogin = "PreLogin"
var instance : UIStoryboard { return UIStoryboard(name: self.rawValue, bundle: Bundle.main) } func viewController<T>(vc : T.Type) -> T where T: UIViewController
{
let identifier = String(describing: T.self) return self.instance.instantiateViewController(withIdentifier: identifier) as! T }}

7. Here we are using Generics for passing type of UIVieController class. T is “placeholder type” of the class to be used, in our case it will be UIViewController custom class(e.g. LoginVC). This function return the class of type T which should be UIViewController only. First of all we will get the Storyboard ID by “String(describing: T.self)” because our Storyboard ID is same as our class name. (e.g. LoginVC). Following is syntax of how to get the LoginVC instance

let loginVC = AppStoryboard.PreLogin.viewController(vc: LoginVC.self)

8. Here we are getting instance of PreLogin Storyboard first by AppStoryboard.PreLogin then we will call viewController(vc: LoginVC.self) function which will get LoginVC using standard system function with instantiateViewController(withIdentifier: identifier) and gives you the instance of LoginVC in just single line of code.

9. Please share and do tap clap if you found this post useful. Happy Coding!!!

--

--