title: How to create a generic function to load a view controller?
body: <p><strong>I have been trying to write a function that works with 3 parameters in order to load a view controller but I haven't been able to make it work.</strong> </p>
  
  <p><em>I'm new into coding</em> and I am reading a tutorial that uses 2 view controllers (login &amp; chat). In order to go from one to the other, the guide tells me to use this whether I want to log in or log out: </p>
  
  <pre><code>@IBAction func loginDidTapped(_ sender: Any) {
  
      // A
      let storyboard = UIStoryboard (name: "Main", bundle: nil)   
      // B
      let logVC = storyboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginViewController   
      // C
      let appDelegate = UIApplication.shared.delegate as! AppDelegate   
      // D
      appDelegate.window?.rootViewController = logVC        
  }
  </code></pre>
  
  <p>If I want to log in or log out I have to change 3 things: </p>
  
  <ol>
  <li><strong>logVC</strong> , <em>which I'd call <strong>var1</em></strong>, used in // B , // D</li>
  <li><strong>"LoginVC"</strong> , <em>which I'd call <strong>var2</em></strong>, used in // B</li>
  <li><strong>LoginViewController</strong> , <em>which I'd call <strong>var3</em></strong>, used in // B</li>
  </ol>
  
  <p>In my newb mind, I imagined that I could create a function that contained the <strong>"A, B, C, D"</strong> lines and let me call it anywhere later just by changing <strong>"var1, var2, var3"</strong> parameters</p>
  
  <hr>
  
  <p>This is what I first created using just <strong>var1</strong> and <strong>var2</strong>: </p>
  
  <pre><code>import Foundation
  import UIKit
  import FirebaseAuth
  
      class Helper {
          static let helper = Helper()
  
          func loadView (var1 : String , var2 : String) {
  
              let storyboard = UIStoryboard (name: "Main", bundle: nil)
              let var1 = storyboard.instantiateViewController(withIdentifier: var2) as! LoginViewController
              let appDelegate = UIApplication.shared.delegate as! AppDelegate
              appDelegate.window?.rootViewController = var1
           }
      }
  </code></pre>
  
  <p>I called it this way and it worked:</p>
  
  <pre><code>Helper.helper.loadView(var1: "logVC" , var2: "LoginVC")
  </code></pre>
  
  <hr>
  
  <p>Now, when I tried to use <strong>var3</strong> in line //B, inside the same function, Xcode underlines var3 and displays this error:</p>
  
  <p><strong>Use of undeclared type 'var3'</strong></p>
  
  <p>This is how it looked after adding a the <strong>var3</strong> parameter:</p>
  
  <pre><code>    func loadView (var1 : String , var2 : String , var3 : String) {
  
                      let storyboard = UIStoryboard (name: "Main", bundle: nil)
                      let var1 = storyboard.instantiateViewController(withIdentifier: var2) as! var3
                      let appDelegate = UIApplication.shared.delegate as! AppDelegate
                      appDelegate.window?.rootViewController = var1
  }
  </code></pre>
  
  <p>This is how I would have called it if it had worked:</p>
  
  <pre><code>Helper.helper.loadView(var1: "logVC" , var2: "LoginVC" , var3: "LoginViewController")
  </code></pre>
  
  <hr>
  
  <p>I'd like to know if is it possible to do what I am imaging since I see functions as equations in which I can replace variables with the information I need.</p>
  
  <p>Thank you all in advance.</p>