Deallocate a ViewController

Hey,

I got a question, I know how "normal" deallocation works and now Iam trying it a bit with ViewControllers,

but no matter what I do nothing works.

I tried it with an empty inital ViewController who just presents another,

but the inital one is still in the memory.


import UIKit

class ViewController: UIViewController {


override func viewDidLoad() {

super.viewDidLoad()

}

override func viewDidAppear(_ animated: Bool) {

dismiss(animated: true)

present(TestController(), animated: true)

}


deinit {

print("deinit")

}

}


So how do I get this ViewController deallocated when Iam presenting the new one ?

Accepted Reply

That is more a Cocoa Touch question than a Swift.


As explained in doc, dismiss should be used by the presenting controller to dismiss the controller it presented.

Here you call on the controller itself, so it will ask its presenting controller to do it. Who presented the controller ?


    override func viewDidAppear(_ animated: Bool) {
        dismiss(animated: true)
        present(TestController(), animated: true)
    }


dismiss should dismiss TestController, but only after it has been presented.

If you call dismiss in TestController class, and put the deinit there, that should work, dismissing TestController.


dismiss(animated:completion:)

Dismisses the view controller that was presented modally by the view controller.

Discussion

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.


You will find interesting discussion here:

https://stackoverflow.com/questions/24668818/how-to-dismiss-viewcontroller-in-swift

Replies

That is more a Cocoa Touch question than a Swift.


As explained in doc, dismiss should be used by the presenting controller to dismiss the controller it presented.

Here you call on the controller itself, so it will ask its presenting controller to do it. Who presented the controller ?


    override func viewDidAppear(_ animated: Bool) {
        dismiss(animated: true)
        present(TestController(), animated: true)
    }


dismiss should dismiss TestController, but only after it has been presented.

If you call dismiss in TestController class, and put the deinit there, that should work, dismissing TestController.


dismiss(animated:completion:)

Dismisses the view controller that was presented modally by the view controller.

Discussion

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.


You will find interesting discussion here:

https://stackoverflow.com/questions/24668818/how-to-dismiss-viewcontroller-in-swift

Thank you very much,

the TestController deinitializer got called and I tried some other stuff and its working perfectly.

Still got one more question, why is the memory usage going up?

For Example my TestControllers View has some Labels and a Button, who dismisses the TestController.

But despite the fact that the TestController got deinitialized, the memory usage is always going up

by some MB, when I present the TestController and dismiss it again.

is deinit being called ?


I remember I read that memory usage does not give an exact figure of the memory effectively used, but cannot be more specific.

You should open a new thread on this question.