Search code examples
iosswiftuicontainerviewchildviewcontroller

Does childViewControllers array have a specific order in a view controller with various containers?


I have a base View Controller with various containers inside it. I use them to control the current step on a process, and only one of them is visible at a time. I created a enum for the steps, and use it's Int rawValue to access the containers corresponding to each step by tag, changing its visibility.

But at some points I have to pass some information to one or more of the step controllers. I was thinking using the same enum approach to access the position of the corresponding View Controller in the childViewControllers array to do that.

Problem is, I haven't found anything in the documentation that ensures me that the childViewControllers array is always at the same order, so I can use this approach.

Does anyone knows it or have a better way achieve something similar?


Solution

  • Maybe would be better to keep order manually, for example by creating Array variable which would be filled with references for your containers/controllers in order which you need.

    Simply connect all container views to outlet collection and hide or unhide certain elements of it

    @IBOutlet var containers: [UIView]!
    

    containers[0].isHidden = true
    containers[1].isHidden = false
    

    For keeping order of controllers just create array variable and fill it with controllers' references. If you're using segues, append destinations of embed segues. If you're adding child controllers by addChild(_:), simply append controllers which you're adding

    var controllers: [UIViewController]