skip to Main Content

I have a storyboard with two view controllers. First one, VC_1, has one button that opens 2nd one – VC_2.
VC_2 also has a button that opens VC_1.
Both controllers have almost identical code:

class VC_1: UIViewController
{ 
   override func viewDidLoad()
    {
        super.viewDidLoad()
        print(“VC_1 loaded")
    }
    
    override func viewDidAppear(_ animated: Bool){  print(“VC_1 appeared")  }
    override func viewDidDisappear(_ animated: Bool){  print(“VC_1 disappeared")  }
    
   
    @IBAction func btnShowVC_2(_ sender: UIButton)
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        secondVC = storyboard.instantiateViewController(identifier: “VC_2”)
        secondVC.modalPresentationStyle = .fullScreen
        show(secondVC, sender: self)
    }
}

The difference is only in "VC_2" instead of "VC_1" in the 2nd controller code.
I have seen this View Controller creation code in Apple documentation and many other examples around the Internet.
When I press the button on the VC_1, I see in the debug window, that VC_2 is loaded and appeared, and VC_1 is disappeared. And same, of course, happens when I press the button on VC_2 – it disappears, and VC_1 is loaded again.
My questions are:

  • what happens with View Controller object after "viewDidDisappear" has been called? Does it really disappear from memory, or "disappear" only means "you cannot see it on the screen?". I do not see "viewDidUnload" in the documentation…
  • I suppose that "viewDidLoad" means that new View Controller object was created in memory. Is there any way to load the View Controller object only once, and then hide and show it without causing "viewDidLoad" to be called? I tried to do it with global variable "secondVC" but got "Application tried to present modally an active controller" error.

2

Answers


    • viewDidDisappear: called after the view is removed from the windows’
      view hierarchy. No, View controller object just left the view property. By the way the amount of memory used by view controllers is negligible. So dont think about too much. If you want to catch when Your View controller object release from the memory put

      deinit { print("vc deallocated") }

    • viewDidUnload, it has been deprecated since the iOS
      6, which used to do some final cleaning.

    • Partly true. Keep in mind ViewDidload called one time for the life cycle of view controller. There is a method called before viewdidload but this is not related with your question.

    Login or Signup to reply.
  1. In addition to "There is a method before viewdidload" -> loadView( ) is a method managed by the viewController. The viewController calls it when its current view is nil. loadView( ) basically takes a view (that you create) and sets it to the viewController’s view (superview).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search