Search code examples
arraysswiftsegueuicontainerviewchildviewcontroller

Why can't I assign data from the root view onto its embedded container view?


I have a DetailViewController that has a container view. The first container view that will appear is the DetailChildViewController.

The DetailChildViewController consists of a collection view which will display the child view's properties. However, even if I assign data onto the properties of the detailChildVC in the prepare(for segue:) function, the properties variable in the DetailChildViewController still returns nil. How do I fix this?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Set the currentViewController to description child VC because that is the first one
        // shown in the container view
        if segue.identifier == "DetailChildSegue" {
            let detailChildVC = DetailChildViewController()
            // Set product of the childVC
            let actionArray = [product?.action.keys.description] as? [String]
            detailChildVC.properties = actionArray
        }
        // Set the currentVC
        currentVC = segue.destination
    }

Solution

  • You assign to a new var DetailChildViewController() not the destination

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier == "DetailChildSegue" {
    
            let detailChildVC = segue.destination as! DetailChildViewController
    
            let actionArray = [product?.action.keys.description] as? [String]
    
            detailChildVC.properties = actionArray
        }
    
        // Set the currentVC
    
        currentVC = segue.destination
    }