0

I am confused by this. It seems the view controller of A is not deallocated when I perform the segue from view A to view B.

When will view controller of A be deallocated in the memory? Can I still accessed the content in view controller of A in the view controller of B?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Patroclus
  • 1,163
  • 13
  • 31

2 Answers2

1

ViewController A will remain in memory.

You shouldn't technically be accessing the content from ViewController A. The proper way to do this is to pass forward any content that ViewController B needs from ViewController A in the prepareForSegue function.

Something like this in ViewControllerA:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewControllerB = segue.destination as? ViewControllerB {
        viewControllerB.content = "" //whatever data you want to pass
    }
}

This is Swift, you'll have to convert it to Objc.

Travis M.
  • 10,930
  • 1
  • 56
  • 72
1

There are two questions here :-

1. When will view controller of A be deallocated in the memory?

Answer When you'll remove it from the stack of view controller without maintaining a reference to it for example if it's a Root View Controller then you'll have change the Root view controller of the window object, If its in a navigation stack you have to pop the view controller from the navigation stack and if you have manually presented it you need to dismiss the view controller.

2. Can I still accessed the content in view controller of A in the view controller of B?

Answer Yes, You can by passing the value of the view controller whether you're doing it by Segue, by pushing it in navigation controller or when you're presenting it but that will limit you to pass data only when your moving to A -> B view controller. However I would like to suggest you to create protocols and delegates to communicate between two view controllers.

This link will demonstrate all means of passing data between view controllers. I hope this information was helpful to you. Thanks

tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49