0

Please take a look at image I have shared. There are 4 ViewControllers in Main.storyboard:

Main.storyboardview

Here is the viewController list:

  • AViewController
  • BViewController
  • CViewController
  • DViewController

And AViewController is an initial ViewController.

  • AViewController has one button which is named 'Take Me To The BViewController'

  • BViewController has two buttons: 'Take Me To The CViewController' and Back Button

  • CViewController has two buttons: 'Take Me To The CViewController' and Back Button

  • DViewController has two buttons: 'Take Me To The AViewController' and Back Button

There are also 4 segues as you see:

  • First segue is A to B
  • Second segue is B to C
  • Third segue is C to D
  • Fourth segue is D to A

I wrote below code for these buttons:

self.performSegue(withIdentifier: "takeMeToTheBViewController", sender : self)
self.performSegue(withIdentifier: "takeMeToTheCViewController", sender : self)
self.performSegue(withIdentifier: "takeMeToTheDViewController", sender : self)
self.performSegue(withIdentifier: "takeMeToTheAViewController", sender : self)

And I wrote below code for the back button action:

self.dismiss(animated: true, completion: nil)

I have 3 questions:

First scenario: Let's assume I open the app. And then I click button in AViewController. This action takes me to the BViewController. And then I click takeMeToTheCViewController button in BViewController. This action takes me to the CViewController. And then I click takeMeToTheDViewController button in CViewController. This action takes me to the DViewController. And then I click takeMeToTheAViewController button in DViewController. This action takes me to the AViewController. Here is the question 1: Can you tell me instances in stack after all these actions? Are below items true for the stack?

  • instance of AViewController(1),
  • instance of AViewController(2),
  • instance of BViewController,
  • instance of CViewController,
  • instance of DViewController

Here is the question 1.1: Please reply this question according to first scenario above. If above instances items are true what should I do to remove below instances from the stack:

  • instance of AViewController(2),
  • instance of BViewController,
  • instance of CViewController,
  • instance of DViewController

Because I don't need them now. I only need AViewController(1) because i am in AViewController currently.

Second scenario: Let's continue on first scenario. I was in first page. Now I click 'takeMeToTheBViewController' button in AViewController. Here is the question 2: Can you tell me instances in stack after all these actions? Are below items true for the stack?

  • instance of AViewController(1),
  • instance of AViewController(2),
  • instance of BViewController(1),
  • instance of BViewController(2),
  • instance of CViewController,
  • instance of DViewController

Third scenario: Let's continue on second scenario. I was in second page. Now I click 'back' button in BViewController(with dissmiss code as you know) This action takes me to the AViewController. Here is the question 3: Can you tell me instances in stack after all these actions? Are below items true for the stack?

  • instance of AViewController(1),
  • instance of AViewController(2),
  • instance of BViewController(1),
  • instance of CViewController,
  • instance of DViewController

I mean instance of BViewController(2) is removed from the stack by clicking back button. Is this true?

Here is the question 4: Would it be a crash problem if these instances accumulate in the stack? Can you reply with question numbers please?

mannyCalavera
  • 563
  • 1
  • 4
  • 16
  • 1
    You might want to consider using a "unwind" operation. Essentially, you create a `@IBAction` at the point to "unwind" (ie `AViewController(1)`), then you setup a segue from the which ever controllers you need to it via the storyboard – MadProgrammer Jan 24 '20 at 06:47
  • @MadProgrammer Is it possible to answer with question numbers so that I can better understand? – mannyCalavera Jan 24 '20 at 06:56
  • 1
    1. Yes 1.1 Use an unwind segue to go from D back to A 2 Yes. 3 Yes. 4. Yes, you’ll run out of memory and your app will quit. – vacawama Jan 24 '20 at 11:31
  • @vacawama Can we print which items are in the stack? (Items means: like an instance of the AViewController, instance of the BViewController etc) Can we print them with print method to see result in output window? If reply is yes, How can I write items in the stack with print method? – mannyCalavera Jan 26 '20 at 21:28
  • If your viewControllers are in a `UINavigationController`, then inside of any viewController, you can `print(self.navigationController!.viewControllers)`. That array is the list of viewControllers on the stack controlled by the `navigationController`. – vacawama Jan 26 '20 at 21:43
  • Thanks. What if my viewControllers are not in a UINavigationController? Should I write print code in AppDelegate.swift in didFinishLaunchingWithOptions method? If yes, Can you also say this please? @vacawama – mannyCalavera Jan 26 '20 at 21:48
  • 1
    You can start with the current VC and ask it for `self.presentingViewController`. You can then ask that one for its `.presentingViewController`, and so on. See [the documentation](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621430-presentingviewcontroller) – vacawama Jan 26 '20 at 21:59

0 Answers0