-4

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
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

5

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?

Mukul More
  • 554
  • 2
  • 5
  • 18
  • I was looking to push to the FourthVC without using mainStoryBoard.instantiateViewController(withIdentifier: "FourthController") as! FourthController (I already know how to do that). I instead tried to use fourthVC = self.navigationController?.viewControllers[3] as! FourthController which got a beyond bounds error. I was perplexed as to why it was out of range so I tried to print out all the child vcs. – Lance Samaria Apr 13 '17 at 13:45
  • navigationController.viewControllers is an array of UIViewControllers which are currently present in the stack. Fourth Vc is not present so it will always give index out of bound option. Do you wish to maintain the state of the Vc . like pop from vc and come back it should be in the same state? – Mukul More Apr 13 '17 at 13:49
  • thanks for the clarification. I don't understand your question " Do you wish to maintain the state of the Vc . like pop from vc and come back it should be in the same state?" – Lance Samaria Apr 13 '17 at 14:14