Skip to content
SwiftMind

UIKit

2 interview questions on uikit.

What's the difference between `viewDidLoad` and `viewWillAppear`, and how do you decide which to use?

JuniorTap to think, then reveal

viewDidLoad runs exactly once per instance, right after the view hierarchy is created. viewWillAppear runs every time the view is about to go on screen — including when you come back from a screen you pushed.

So the split is one-time construction versus repeated refresh. Adding subviews, activating constraints, setting fonts and colours, wiring up targets: all viewDidLoad, because doing it again would duplicate work and can duplicate subviews. Anything that could have changed while the screen was away goes in viewWillAppear — re-reading a model, updating a badge, restoring navigation bar state, reflecting a setting the user changed two screens deep.

The bug you get from mixing them up is very recognisable: put a data refresh in viewDidLoad and everything looks right on first launch, then the user edits something on a detail screen, comes back, and the list still shows the old value.

For work that should start only once the transition is actually finished — animations, screen-view analytics, becoming first responder — I'd use viewDidAppear, since the view is genuinely on screen by then.

What an interviewer listens for

  • States once-per-instance versus every-appearance clearly
  • Assigns construction to viewDidLoad and refresh to viewWillAppear
  • Names the stale-data bug that results from confusing them
  • Knows viewDidAppear is for post-transition work

Follow-ups

Why might a view controller's `viewDidLoad` never run?

Because the view was never accessed. view is lazy, so a controller that's created but never presented, and whose view nobody touches, never loads. It also means the reverse surprise: reading .view from outside to configure something loads it right then, earlier than you expected.

Read the concept: UIViewController Lifecycle

Walk me through what happens between a finger touching the glass and your action method running.

MidTap to think, then reveal

Two passes, in opposite directions.

First, hit testing, going down. UIKit calls hitTest(_:with:) on the window, which asks point(inside:with:) and then recurses into subviews back-to-front, returning the deepest view that contains the point. Views that are hidden, have user interaction disabled, or have alpha at or below 0.01 are skipped entirely. The result is the view the touch is delivered to.

Then the responder chain, going up. If that view doesn't handle the event, it goes to its next responder — normally the superview, then the view controller at the top of its hierarchy, then the parent or presenting controller, then the window, the application, and finally the app delegate. The first responder that implements the action handles it and the walk stops. If nobody does, the event is silently discarded.

For a target-action button with an explicit target, the walk is skipped — it goes straight there. The chain matters when the target is nil, which is how the system edit menu and the keyboard-dismissal trick work without knowing which field is focused.

The bug this explains most often is a button outside its parent's bounds: it's visible because the parent doesn't clip, but untappable because hit testing rejected the point at the parent before it ever looked at the button.

What an interviewer listens for

  • Separates the downward hit-test pass from the upward responder walk
  • Knows the skip conditions: hidden, interaction disabled, low alpha
  • Describes the chain beyond the view hierarchy — controller, window, app
  • Notes unhandled events are discarded silently
  • Applies it to a real bug

Follow-ups

How would you make a button outside its parent's bounds tappable?

Override point(inside:with:) on the parent to also return true for points inside the button's frame, so hit testing recurses. Enlarging the parent or restructuring the hierarchy is usually cleaner if you can — the override is easy to forget about when someone later changes the layout.

Read the concept: The Responder Chain

Drill interview-level questions

Advanced and senior quiz questions only, across every category.

Start