Sobes.tech
Middle

Tell about the lifecycle of a controller and how they are invoked.

sobes.tech AI

Answer from AI

The lifecycle of UIViewController includes methods called at specific moments of its existence: from creation to destruction.

Main lifecycle methods:

  1. viewDidLoad():

    • Called after the controller's view is loaded into memory but before it appears on the screen.
    • Used for initializing data, setting up UI elements that do not depend on the view's geometry.
    • Called only once during the controller's lifetime.
  2. viewWillAppear(_ animated: Bool):

    • Called just before the controller's view appears on the screen.
    • Used for tasks that need to occur every time the view becomes visible (e.g., data updates, animations).
    • Can be called multiple times.
  3. viewDidAppear(_ animated: Bool):

    • Called after the view has fully appeared on the screen and is visible to the user.
    • Suitable for starting animations or other tasks that require the view to be fully ready.
    • Can be called multiple times.
  4. viewWillDisappear(_ animated: Bool):

    • Called just before the view is removed from the screen (e.g., when transitioning to another screen).
    • Used for saving state or stopping tasks that should not run while the view is invisible (e.g., stopping timers).
    • Can be called multiple times.
  5. viewDidDisappear(_ animated: Bool):

    • Called after the view has been completely removed from the screen.
    • Used for cleaning up resources or stopping tasks that are not needed when the view is invisible.
    • Can be called multiple times.
  6. viewWillLayoutSubviews():

    • Called before the controller starts laying out its subviews.
    • Used for making changes to constraints or frames before auto layout occurs.
    • Can be called multiple times.
  7. viewDidLayoutSubviews():

    • Called after the controller has finished laying out its subviews.
    • Used for tasks that depend on the final sizes of subviews (e.g., adjusting scroll indicators or shadows).
    • Can be called multiple times.
  8. didReceiveMemoryWarning():

    • Called when the system is running low on memory.
    • Used to release resources that can be recreated later.
  9. deinit:

    • Called just before the UIViewController object is destroyed and deallocated.
    • Used for final cleanup, such as removing observers or closing network connections.

Call order:

Typical order of method calls when a controller appears:

  1. viewDidLoad()
  2. viewWillAppear(_ animated: Bool)
  3. viewWillLayoutSubviews()
  4. viewDidLayoutSubviews()
  5. viewDidAppear(_ animated: Bool)

Typical order of method calls when a controller disappears:

  1. viewWillDisappear(_ animated: Bool)
  2. viewDidDisappear(_ animated: Bool)
  3. deinit (if the controller is no longer needed)

Implementation example:

import UIKit

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Setup UI, initialize data
        print("viewDidLoad")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // Update UI before appearing
        print("viewWillAppear")
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // After appearing, start animations
        print("viewDidAppear")
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // Prepare for disappearance, save state
        print("viewWillDisappear")
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        // Disappearance completed, cleanup
        print("viewDidDisappear")
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        // Before auto layout
        print("viewWillLayoutSubviews")
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        // After auto layout
        print("viewDidLayoutSubviews")
    }

    deinit {
        // Cleanup resources
        print("deinit")
    }
}