In iOS development with UIKit, every UIViewController goes through a clear and predictable life cycle. It all starts when the controller’s view is first loaded into memory, continues as it appears on screen, and ends when it’s no longer visible or needed. Understanding this life cycle helps you know the right place to set up your UI, load data, and clean up resources. Each phase from viewDidLoad to viewWillAppear, viewDidAppear, and eventually viewWillDisappear and viewDidDisappear gives you a chance to hook into what’s happening and respond appropriately.
Understanding iOS lifecycle helps you get better at:
- Loading data
- Updating UI
- Managing memory
- Debugging navigation issues
UIViewController Life Cycle Flow in iOS
Here’s the sequence of key methods in the UIViewController life cycle:
1. init(coder:) or init(nibName:bundle:)
- Called when a view controller is initialized.
- You rarely override this unless doing custom setup at creation.
2. loadView()
- Creates the view hierarchy programmatically.
- You usually don’t override this if using Storyboards/XIBs.
3. viewDidLoad()
This runs when the view is loaded into memory.
Use it to:
- Do setup that only needs to happen once
- Create and set up views
- Make early network calls or load basic data
override func viewDidLoad() { super.viewDidLoad() print("✅ viewDidLoad called")
}
4. viewWillAppear(_:)
Called just before the view shows up on screen.
Use it to:
- Refresh data or update the UI
- Start basic animations
- Show or hide parts of the UI as needed
5. viewDidAppear(_:)
Gets called after the view is fully on screen.
Best used for:
- Running tasks that require the view to be visible
- Logging screen views for analytics
- Starting animations or timers that shouldn’t run off-screen
6. viewWillDisappear(_:)
Called just before the view is removed (e.g., navigating away).
Ideal for:
- Saving state
- Stopping animations or video
- Dismissing keyboards
7. viewDidDisappear(_:)
Called after the view has been removed from the screen.
Ideal for:
- Releasing resources
- Stopping services (e.g., location, camera)
Additional Life Cycle Methods
Method | Purpose |
deinit | Called when the view controller is deallocated (used to release resources). |
didReceiveMemoryWarning() | Handle low memory warning (rare in modern apps). |
viewWillLayoutSubviews() | Called before laying out subviews (safe to update constraints). |
viewDidLayoutSubviews() | Called after laying out subviews. |
Summary Table
Method | Called When |
init() | When the VC is initialized |
loadView() | When the view is created manually |
viewDidLoad() | Once, after the view loads |
viewWillAppear() | Every time the view is about to appear |
viewDidAppear() | Every time the view has appeared |
viewWillDisappear() | Before the view goes off-screen |
viewDidDisappear() | After the view has disappeared |
deinit | When VC is deallocated |
Final Words
The ViewController life cycle lets you control what happens as a screen appears, updates, and disappears. It’s key for loading data, updating UI, and freeing up resources. When you hire iOS developers who know this flow well, your app runs smoother and stays more stable.