Skip to content
tezvyn:

The UIViewController Lifecycle: From Creation to On-Screen

Source: developer.apple.comEasyHow cards are made

The UIViewController Lifecycle: From Creation to On-Screen

Think of a UIViewController as an actor with cues to enter, perform, and exit. Its lifecycle methods are your script for setting up data, updating the UI, and cleaning up resources.

Why it exists

The UIViewController lifecycle provides a structured, predictable way to manage a screen's resources. Without it, developers would have to manually track when a view is on-screen, off-screen, or being destroyed, leading to memory leaks, stale data, and inconsistent UI.

The mental model

Think of a UIViewController as an actor in a play. The view is its costume and props. The lifecycle methods are cues from the director (iOS) telling the actor when to prepare, perform, and exit. viewDidLoad is getting into costume backstage. viewDidAppear is stepping into the spotlight. viewWillDisappear is the cue to exit stage left.

How it works

iOS calls a sequence of methods on your view controller as its state changes. The key events are:

init: The controller object is created from a storyboard or code. Its view has not been created yet.

viewDidLoad: The view hierarchy is loaded into memory. This method is called only once in the controller's lifetime. It's the ideal place for one-time setup, like adding subviews, setting static text, or starting a network request for initial data.

viewWillAppear: The view is about to be added to the screen. This is called every time the view is about to become visible, which could be many times. Use it to refresh data or UI that might have changed while the screen was hidden.

viewDidAppear: The view is fully on-screen and visible to the user. Use this to start animations, video playback, or other intensive tasks that you don't want to delay the screen's appearance.

viewWillDisappear: The view is about to be hidden or dismissed. This is your chance to save user input, stop animations, or invalidate timers.

viewDidDisappear: The view is now fully off-screen. Use this for final cleanup of tasks started in viewDidAppear.

When to use it

You override these methods in every UIViewController subclass to manage the screen's behavior. It's the standard way to populate views with data, respond to visibility changes, and clean up resources.

When not to use it

You should never call these lifecycle methods directly on your own view controller instances (e.g., myController.viewDidLoad()). This is the system's job. Forcing a lifecycle call will break the expected order of events and cause unpredictable bugs.

One canonical example

Imagine a user profile screen. In viewDidLoad, you make a single network call to fetch the user's details. In viewWillAppear, you might update a "last active" label, since this should refresh every time the screen appears. When the user navigates away, viewWillDisappear is the perfect place to save any unsaved changes to their profile.

Interview question

Which UIViewController lifecycle method is best for refreshing data or UI that might have changed while the view was hidden?

  • a.viewWillAppearCorrect
  • b.viewDidLoad
  • c.viewDidAppear
  • d.init
Why?

viewWillAppear is called every time the view is about to become visible, making it ideal for refreshing data or UI that might have changed. viewDidLoad is only called once when the view is first loaded, not for subsequent refreshes.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles