iOS Passing Data between View Controllers By assigning property (Passing data forward)


You can pass data directly by assigning the property of the next view controller before you push or present it.

class FirstViewController: UIViewController {

    func openSecondViewController() {

        // Here we initialize SecondViewController and set the id property to 492
        let secondViewController = SecondViewController() = 492

        // Once it was assign we now push or present the view controller
        present(secondViewController, animated: true, completion: nil)


class SecondViewController: UIViewController {
    var id: Int?

    override func viewDidLoad() {

        // Here we unwrapped the id and will get the data from the previous view controller.
        if let id = id {
            print("Id was set: \(id)")