UIKit: View Controllers and Their Lifecycle
Why Learn UIKit?
Many professional apps still contain UIKit code. You will read it, fix it and mix it with SwiftUI, so you should know the basics.
UIViewController
A view controller manages one screen. It owns a view and responds to events.
swift
import UIKit
class HelloViewController: UIViewController {
private let label = UILabel()
private let button = UIButton(type: .system)
private var taps = 0
override func viewDidLoad() {
super.viewDidLoad() // runs once, when the view is loaded
view.backgroundColor = .systemBackground
title = "Hello UIKit"
label.text = "Taps: 0"
label.font = .preferredFont(forTextStyle: .title1)
label.textAlignment = .center
button.setTitle("Tap me", for: .normal)
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
let stack = UIStackView(arrangedSubviews: [label, button])
stack.axis = .vertical
stack.spacing = 20
stack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stack)
NSLayoutConstraint.activate([
stack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stack.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
@objc private func tapped() {
taps += 1
label.text = "Taps: \(taps)"
}
}
The Lifecycle
| Method | When it runs | Typical use |
|---|---|---|
viewDidLoad | Once, after the view is created | Create views, one-time setup |
viewWillAppear | Each time before the screen shows | Refresh data, update navigation bar |
viewDidAppear | After the screen is visible | Start animations, analytics |
viewWillDisappear | Before it leaves | Save changes, stop work |
viewDidDisappear | After it left | Release resources |
viewDidLayoutSubviews | After layout changed | Frames that depend on final size |
Always call super: In every lifecycle method call
super.viewWillAppear(animated) and the others. Forgetting it can cause odd bugs.
Start the App with a Navigation Controller
swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UINavigationController(rootViewController: HelloViewController())
window.makeKeyAndVisible()
self.window = window
}
}
Moving Between Screens
swift
// Push (navigation controller)
navigationController?.pushViewController(DetailViewController(), animated: true)
navigationController?.popViewController(animated: true)
// Present modally
let vc = SettingsViewController()
vc.modalPresentationStyle = .pageSheet
present(vc, animated: true)
dismiss(animated: true)
Passing Data and Delegates
swift
protocol ColorPickerDelegate: AnyObject {
func didPick(color: UIColor)
}
class PickerViewController: UIViewController {
weak var delegate: ColorPickerDelegate? // weak avoids a retain cycle
func choose() {
delegate?.didPick(color: .systemRed)
dismiss(animated: true)
}
}
Tip: Storyboards are optional. Many teams build UIKit screens in code because it works better with version control. Both are valid.
Conclusion
A UIViewController manages a screen and has a clear lifecycle. Build views in viewDidLoad, refresh in viewWillAppear and use delegates to send results back.
Quick Summary: viewDidLoad runs once, viewWillAppear runs each time. Use navigationController for push and present for modal screens.

