1

I would like to have a set up where info is passed between viewControllers as follows: 1->2->3->2. Currently I have a set up to pass info from VC 1->2. The info being passed is the indexPath that a cell is selected from a table. The info needs to be passed because the code in the 2nd viewController' viewDidLoad() depends on the cell selected. It is set up as follows (and works fine): VC 1:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "cellSelected" else {return}
        let indexPath = self.tableView.indexPathForSelectedRow
        let rowSelectedd = indexPath
        let destViewController: MainPageCellSelectedViewController = segue.destination as! MainPageCellSelectedViewController
        destViewController.rowSelected = rowSelectedd!
    }
    var rowSelectedd = IndexPath()
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "cellSelected", sender: rowSelectedd)
    }

VC 2 (using the info that is passed): var rowSelected = IndexPath() //this has info prepared for from the MainPageVC

override func viewDidLoad() {
    super.viewDidLoad()

    if rowSelected.section == 0{
        //do something
    } else{
        // do something else
    }
}

In this 2nd viewController there is a button that segues to a third viewController (this also works fine). However, in that third viewController there is a back button that segues back to the 2nd viewController. The problem is, the 2nd viewController's viewDidLoad() code depends on the variable that was passed from the 1st VC. Since it isn't being passed (because the segue is now from 3->2 instead of 1->2) the app crashes. I am not sure what is the best way to transfer this info appropriately. How should it be done?

Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Rob Feb 11 '17 at 17:18

1 Answers1

1

You don't really use segues to navigate backwards unless you're using an Unwind segue. From what you described, it sounds like delegation is what you want.

Try something like this:

protocol ThirdVCDelegate {
    func somethingHappens()
}

class SecondVC: UIViewController {
    override func prepare(for segue:UIStoryboardSegue, 
                                   sender: Any?) 
    {
        if segue.identifier == "whatever the segue is between second and third VC" {
            let thirdVC = segue.destination as! ThirdVC
            thirdVC.delegate = self
        }
    }
}

extension SecondVC: ThirdVCDelegate {
    func somethingHappens() {
        // Do whatever you want in SecondVC in response to some action in ThirdVC
    }
}

class ThirdVC: UIViewController {
    var delegate:ThirdVCDelegate!

    func someMethodWhereYouWantToMakeACallbacktoSecondVC {
       delegate.somethingHappens()
       // You can pop or dismiss your thirdVC if you're done with it here.
    }
}

You can also add parameters to the protocol method if you want to pass a specific value from thirdVC to secondVC.

Alvin Abia
  • 1,221
  • 1
  • 17
  • 18
  • Also highly recommend reading up on delegation in iOS if you haven't done so already because you'll be using it often in development. – Alvin Abia Feb 11 '17 at 17:16