4

I have been following along a video tutorial that is teaching me about programmatically creating an iOS App without storyboards.

It is going well and I like most of the things about it, however I have a slight problem.

I am only half way through and I have noticed a potential problem with memory, the way the tutorial has shown me is that for every next view I present it like so:

let ChooseCategoryController = ChooseCategoryViewController()
present(ChooseCategoryController, animated: true, completion: nil)

and then I present another one from the view there. And to go back I am also presenting another view. I noticed when running the app that the memory usage is going up and up.

Is there a way, that when I present a new view, I can detect what views are open and close all the others?

I thought maybe something in the completion section but I am struggling as I am rather new.

Thanks in advance.

Update1

After trying the solution below and adding in the self. as xcode asked me to I have this code:

 dismiss(animated: false, completion: {
        self.present(DisplayQuestionsViewController(), animated: true, completion: nil)
    })

I get this warning:

Warning: Attempt to present < triviaGameApp.DisplayQuestionsViewController: 0x7fb39481ce00 > on < triviaGameApp.ChooseCategoryViewController: 0x7fb392e019f0 > whose view is not in the window hierarchy!

If it is relevant. This code is fired on the click of a UICollectionView cell on ChooseCategoryViewController()

Update 2:

I have changed the code now to the following:

dismiss(animated: false, completion: {
        self.parent?.present(DisplayQuestionsViewController(), animated: true, completion: nil)
    })

As mentioned by the poster below. The current view controller does now dismiss however it does not load the new one.

Community
  • 1
  • 1
JamesG
  • 1,552
  • 8
  • 39
  • 86

1 Answers1

3

You can, but you will have to loops through all the viewControllers in the windows and delete it. I would say it is dangerous and really not recommended.

Instead, I like to suggest this. You dismiss your old controller and at the same time present a new one.

dismiss(animated: false, completion: {
    self.parent?.present(MyOtherViewController(), animated: true, completion: nil
})

The above code use the completion block to ensure that when the current ViewController is dismissed, it immediately present the next one MyOtherViewController in this case.

Zac Kwan
  • 5,587
  • 4
  • 20
  • 27
  • Thanks, I just need to pop out. I will give that a try and post results. If all works I will accept :) – JamesG Nov 24 '16 at 14:51
  • Hi, i updated the post. Should be using `parentViewController` instead to present the new `ViewController` as the current one is being `dismiss`. That was also what the warning is trying to tell you. – Zac Kwan Nov 24 '16 at 15:35
  • Ultimately there is multiple way of showing different view controller. Maybe you want to look at UINavigationController as well. With that, you can use `push` and `pop` like what most app is doing out there. – Zac Kwan Nov 24 '16 at 15:36
  • your update also does not do anything. It just closes the current view but does not load the second one :/ Any ideas? Appreciate your help btw – JamesG Nov 24 '16 at 16:21
  • Hey @JamesG `dismiss` would only work if your `ViewController` is being `present`. It wouldn't work when it is the `rootViewController` of the `window`. I need to understand your view hierarchy better to be able to help you. Maybe this link will give u a better understanding: https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-navigation-controllers-and-view-controller-hierarchies--cms-25462 – Zac Kwan Nov 25 '16 at 03:19