UIKit: View Controllers and Their Lifecycle

IOS Development 11 min min read Updated: Nov 16, 2026 Intermediate
UIKit: View Controllers and Their Lifecycle
Intermediate Topic 1 of 4

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

MethodWhen it runsTypical use
viewDidLoadOnce, after the view is createdCreate views, one-time setup
viewWillAppearEach time before the screen showsRefresh data, update navigation bar
viewDidAppearAfter the screen is visibleStart animations, analytics
viewWillDisappearBefore it leavesSave changes, stop work
viewDidDisappearAfter it leftRelease resources
viewDidLayoutSubviewsAfter layout changedFrames 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators