Search code examples
iosswiftprotocols

Passing variables back from a ViewController to a previous one, but variables not updating?


I have two view controllers which I am interested in passing variables from one view controller to the next in a backwards manner. To achieve this I used a protocol, however, the variable in the first view controller are not updating when going back from view controller two to view controller one:

Below is my code for the first view controller:

    import UIKit

    class BlueBookUniversalBeamsVC: UIViewController {

        var lastSelectedTableRowByTheUser: Int = 0

        var lastSelectedTableSectionByTheUser: Int = 0

    override func viewDidLoad() {

            super.viewDidLoad()

            print(lastSelectedTableRowByTheUser)

            print(lastSelectedTableSectionByTheUser)

     }

 }

    extension BlueBookUniversalBeamsVC: ProtocolToPassDataBackwardsFromDataSummaryVcToPreviousVc {

        func dataToBePassedUsingProtocol(passedSelectedTableSectionNumberFromPreviousVc: Int, passedSelectedTableRowNumberFromPreviousVc: Int) {

            self.lastSelectedTableRowByTheUser = passedSelectedTableRowNumberFromPreviousVc

            self.lastSelectedTableSectionByTheUser = passedSelectedTableSectionNumberFromPreviousVc

            print("Last selected row passed back from SummaryVC is equal to \(passedSelectedTableRowNumberFromPreviousVc)")

            print("Last selected section passed back from SummaryVC is equal to \(passedSelectedTableSectionNumberFromPreviousVc)")

    }

Below is my code inside the second view controllerL

import UIKit

class BlueBookUniversalBeamDataSummaryVC: UIViewController {

   var delegate: ProtocolToPassDataBackwardsFromDataSummaryVcToPreviousVc?

   @objc func navigationBarLeftButtonPressed(sender : UIButton) {

        let main = UIStoryboard(name: "Main", bundle: nil)

        let previousViewControllerToGoTo = main.instantiateViewController(withIdentifier: "BlueBookUniversalBeamsVC")

        if delegate != nil {

            delegate?.dataToBePassedUsingProtocol(passedSelectedTableSectionNumberFromPreviousVc: self.selectedTableSectionNumberFromPreviousViewController, passedSelectedTableRowNumberFromPreviousVc: self.selectedTableRowNumberFromPreviousViewController)

        }

        self.present(previousViewControllerToGoTo, animated: true, completion: nil)

    }

}

The weird thing is that in Xcode console when I go back from VC2 to VC1, inside the protocol function extension in VC1 I can see the values being printed correctly. However, when the values get printed from inside viewDidLoad() both of them are showing as 0. Any idea why this is happening, is there something I am missing out here?


Solution

  • Your second view controller is instantiating a new instance of the first view controller rather than using the instance that was already there. The second view controller shouldn’t present the first view controller again, but rather dismiss (or pop) back to it, depending upon the first presented or pushed to it.

    By the way, the delegate property of the second view controller that points back to the first one should be a weak property. You never want a child object maintaining a strong reference to a parent object. Besides, delegates are almost always weak...