0

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?

GustavoAzOl
  • 299
  • 2
  • 12

3 Answers3

1

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]
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • I edited the question to clarify more of the problem. But thinking about the visibility of the views, your solution seems way better than the tag approach. – GustavoAzOl Mar 22 '19 at 20:31
0

Yes it's ordered

let v = Vvv()

self.addChild(v)

let d = Ddd() 

self.addChild(d)

print(self.children)

[dfjkfjdjdjdfjfdkas.Vvv: 0x1041086e0,dfjkfjdjdjdfjfdkas.Ddd: 0x104012060]


class Vvv:UIViewController {

} 
class Ddd:UIViewController {

}

enter image description here

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • But I added all of the child controllers in storyboard, by embed segues. Even in this case, I can guarantee always the same order? – GustavoAzOl Mar 22 '19 at 19:58
  • I tried that test too, and for me it seemed that they appeared in the same order. Just wanted some confirmation that it would work the same way in all iOS versions. – GustavoAzOl Mar 22 '19 at 20:33
0

If you have your container views connected via IBOutlets, and you want to track them with an "ordered" array, you could do this:

class ContainersViewController: UIViewController {

    @IBOutlet var containerA: UIView!
    @IBOutlet var containerB: UIView!
    @IBOutlet var containerC: UIView!

    var orderedContainers = [UIView]()

    override func viewDidLoad() {
        super.viewDidLoad()

        orderedContainers = [containerA, containerB, containerC]

    }

}

From there on out, you can use indexing, as in:

orderdContainers[1].isHidden = true
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • I edit the question to clarify. The old one was a bit misleading. – GustavoAzOl Mar 22 '19 at 20:28
  • @GustavoAzOl - see this answer https://stackoverflow.com/a/48892520/6257435 – DonMag Mar 22 '19 at 20:32
  • Seeing the answer, yes I could use the prepare for segue to store the references... But I was thinking that, if could use the already present childViewControllers, there would be no need to store more references to the controllers and I could try something like combining the storyboard segues/child order and array of container answers posted here. – GustavoAzOl Mar 22 '19 at 20:46
  • 1
    Relying on the order in which they were added in your Storyboard can be problematic. Suppose you start with 4 "steps" and then later decide you want a new "step" between 2 and 3? You could remove / re-add your container views, or you could re-assign the segues... But it's also possible future versions of Xcode / iB could change that. Might be better to explicitly keep track of what you have going on. – DonMag Mar 22 '19 at 20:57