Search code examples
iosswiftuiviewcontrolleruinavigationcontrollerchildviewcontroller

Swift iOS -How to print a list of all Child View Controllers in Navigation Stack that haven't been pushed on yet


I have a navController with 4 vc's:

navigationController
FirstController
SecondController
ThirdController
FourthController

When I'm on the FirstController I want to print out a list of all the child vcs that haven't been pushed on the stack.

I tried:

let allVCs = self.navigationController!.childViewControllers
print(allVCs.description)

I also tried:

let allVCs = self.navigationController!.viewControllers
print(allVCs.description)

In both cases I only get a print statement of ProjectName.FirstController. I realize I get that because that's the only one currently on the stack.

How do I find out the rest of the child vcs even though they haven't been pushed on the stack as of yet?

//print statements I'm looking to get
ProjectName.FirstController
ProjectName.SecondController
ProjectName.ThirdController
ProjectName.FourthController

Solution

  • Navigation controller cannot predict future and tell which controllers will get pushed on to the stack. So its not possible to get a list of controllers which have not been added on the stack. After adding you can get all the details.

    Though the question arises here is why do you want info the view controllers that are not loaded in the stack?