WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

What is a UIViewController

Swift version: 5.10

Paul Hudson    @twostraws   

iOS developers often like to talk about the Model-View-Controller architecture (MVC), where every component in their code is either a Model, a View, or a Controller. This is the standard architecture on iOS, which makes it strange that one of the first types you meet is UIViewController – it has both “view” and “controller” in its name, so what is it?

Well, there is no single answer. In the early days of iPhones, UIViewController represented one screen of content. Your mailbox was one view controller, and when you tapped to read a message that was shown in a different view controller.

However, the story is more complex than that, because of view controller containment – you can put one view controller inside another, which helps break up your code. As a result, one screen of content might consist of four or five view controllers all working together.

The main (and inescapable) role of view controllers is to respond to view lifecycle event. That is, your view controller code will get call when your view is created, shown, hidden, and destroyed, so it’s down to you to write code to respond to those events properly.

Some people make their view controllers more view than controller (i.e., make it handle layout code and as little else as possible), some people make it more controller than view (i.e., put layout code into UIView subclasses and put glue code into their view controllers), and some people make it do both: they put all their view code and all their controller code in one place.

If you want to make your view controller more view than controller that’s fine. If you want to make it more controller than view that’s less fine, but it can still work. If you want to use it as both, chances are you’ll stop using Model-View-Controller and start using Massive-View-Controller instead.

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Available from iOS 2

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.3/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.