Q:

How to programmatically navigate to ViewController and pass data

I'm trying to pass data to the second view controller without success,
This is my code:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: “secondVC")
self.present(vc, animated: true, completion: nil)

//The following solution as I checked in the previous questions doesn't work:

vc.Var1InSecondViewController = self.var1
vc.Var2InSecondViewController = self.var2

It claims that 'vc' which is related to the second view controller has no member of 'Var1InSecondViewController' even though I did created it in the second view controller.

Your help would be appreciated, thank you.

A:

You have to specify the class name of the secondVC as follows in order to access its properties,
Instead of,
let vc = storyboard.instantiateViewController(withIdentifier: “secondVC")

Use
let vc = storyboard.instantiateViewController(withIdentifier: “secondVC") as? YOUR_SECOND_VIEW_CONTROLLER_CLASS_NAME

And do the assignment steps before presenting the view controller as follows,
vc?.Var1InSecondViewController = self.var1
vc?.Var2InSecondViewController = self.var2
self.present(vc!, animated: true, completion: nil)

