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:
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
didReceiveMemoryWarning():- Called when the system is running low on memory.
- Used to release resources that can be recreated later.
-
deinit:- Called just before the
UIViewControllerobject is destroyed and deallocated. - Used for final cleanup, such as removing observers or closing network connections.
- Called just before the
Call order:
Typical order of method calls when a controller appears:
viewDidLoad()viewWillAppear(_ animated: Bool)viewWillLayoutSubviews()viewDidLayoutSubviews()viewDidAppear(_ animated: Bool)
Typical order of method calls when a controller disappears:
viewWillDisappear(_ animated: Bool)viewDidDisappear(_ animated: Bool)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")
}
}