0

I am trying to pass a variable through a few different classes to use in core data (one to many relationships).

To give some context of what is happening. I have a tableViewController (jobs), when a user clicks on one of the cells it takes them to a tabBarController with 4 viewControllers. Then on the first viewController is a tableView, if the user clicks on a specific cell it takes them to another tabBarController. I am trying to pass the variable

var job: Job?

from the top level to the final tabBarController but it seems to be getting lost when trying to pass the variable from the first TabBarcontroller to the second tabBarController. I am struggling to see what is wrong to not pass the variable through the ViewControllers.

Here each of the ViewControllers variables and how I am passing them:

JobViewController:

var jobs = [Job]()

// CELL DID SELECT ROW
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let jobTab = jobTabController()
        jobTab.job = jobs[indexPath.row]
        show(jobTab, sender: self)
    }

This is the first tabBarController - jobTabController:

var job: Job?

This is the first ViewController that is attached to jobTabController - jobInfo:

// OBSERVE JOB
var job: Job? {
    let tabBarController = self.tabBarController as! jobTabController
    return tabBarController.job
}

    // CELL DID SELECT ROW
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            if indexPath.row == 1 {
                let camLens = cameraLensesTabController()
                camLens.job = self.job
                show(camLens, sender: self)
            }
        }
    }

NOTE: This is where the variable seems to get lost - between jobInfo & cameraLensesTabController - name: cameraLensesTabController

var job: Job?

Then this is my viewController that is attached to cameraLensesTabController - cameraController:

var job: Job?

@IBAction func HandleAddCamera(sender : UIButton) {
        let newCamera = newCameraController()
        newCamera.job = self.job
        let navController = UINavigationController(rootViewController: newCamera)
        present(navController, animated: true, completion: nil)
    }

On the cameraController I have an add button that takes you to another ViewController named newCameraController where you can add items to a tableView on cameraController:

var job: Job?
rmaddy
  • 314,917
  • 42
  • 532
  • 579
spoax
  • 471
  • 9
  • 29

1 Answers1

1

In the jobInfo controller, you access var job via tabBarController.job. But in the cameraLensesTabController you have a var Job? that is apparently initialised to nil, like in the cameraController.
Did you expect that these two controllers can access the original value of tabBarController.job? Apparently, you did never forward it to them.

EDIT:

When a viewController performs a segue (i.e. switches to) to another one, it calls first prepareForSegue:(UIStoryboardSegue *)segue sender:(id)senderor its Swift equivalence. In this function, you can access the destination viewController and set properties there, please see https://stackoverflow.com/a/7865100/1987726.

EDIT 2:

The prepareForSegue:sender: method is called in any case. Please lookup Perform Segue programmatically and pass parameters to the destination view.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • Yes, I want to access the original value from `JobViewController`. How do I forward it to cameraLensesTabController instead of it being initialised to nil? – spoax May 01 '18 at 14:51
  • Is this the same when doing it programmatically? I am trying to forward the job in the function `cellDidSelectRow` in the VC called `jobTabController`. – spoax May 01 '18 at 15:32