Home / view controller lifecycle interview questions

UIKit

View controller lifecycle interview questions

The view controller lifecycle is a UIKit staple because putting work in the wrong method causes real, subtle bugs.

The lifecycle methods and their jobs

viewDidLoad runs once when the view loads, so it is for one-time setup. viewWillAppear and viewDidAppear run every time the view appears, so they suit refreshing data, starting animations, or tracking screen views. viewWillDisappear and viewDidDisappear handle teardown like pausing work or saving state. loadView creates the view hierarchy when you build it in code.

Where bugs come from

Interviewers probe mistakes like doing per-appearance work in viewDidLoad so it never refreshes, or expensive work in viewDidLoad that delays presentation. Discuss that geometry-dependent work belongs in viewDidLayoutSubviews since bounds are not final in viewDidLoad, and that failing to balance start and stop calls across appear and disappear leaks work or observers.

Go deeper than a summary

Share Your Screen works topics like this through in full: 75 solved Swift problems with tests, 9 real case studies, and 7 annotated mock interviews, so you rehearse the live round instead of just reading about it.

Get the book

Frequently asked

viewDidLoad or viewWillAppear?

Use viewDidLoad for one-time setup that runs once; use viewWillAppear for work that must repeat every time the view appears, like refreshing data.

Why not do layout math in viewDidLoad?

The view's bounds are not final there. Layout-dependent work belongs in viewDidLayoutSubviews, which runs after Auto Layout has sized the views.