UIViewControllers Flashback

Sue Cho
The Startup
Published in
4 min readOct 18, 2020

--

Article with music : Before You Go — Lewis Capaldi

Isn’t that a bit dangerous…?

The very first thing I learned about iOS development was HIG and some of the wonderful details the APPLE EMPIRE insists. (The details definitely were enough to persuade me to keep studying iOS.) After filling up the motivation gauge, the next thing I knew was that I was confronting View Controllers. After doing several projects I thought I totally understood what ViewControllers are and their roles. But do I? So I flipped back to the chapters of what I have learned a few months ago when I first started iOS development.

class UIViewController: UIResponder

UIViewController is a class that inherits UIResponder and takes the role of managing a view hierarchy for UIKit app. There are 2 types of View Controller : Content view controllers and Container view controllers. While content view controllers are the main type of view controllers that we create, container view controllers takes the role of presenting its child view controllers in certain way. So if you take a look at the image ‘Does-Nothing-Project’ underneath, the navigation controller is the container view controller. Regardless of the type the main responsibilities of a view controllers include:

  • Responds to user interaction across the screen(or along the responder chain)
  • Acts as an intermediary between the views it manages and the underlying data.
  • Deals with resizing of views and rotation of screen(Orientation change).

Multiple view controllers are used throughout the project thus making it inevitable for an app to contain more than one custom subclass of UIViewControllers. As these custom view controllers define the overall flow of the app VCs tend to get massive. Thus, it’s important to divide the role by making concrete decisions of who will take the responsibility of what.

Life Cycle

There are times when it gets so confusing when to call a specific method that is dependent on view controller life cycle before instantiating/popping a view controller. So let’s take a look at the life cycle.

https://developer.apple.com/documentation/uikit/uiviewcontroller
Does-Nothing-Project

I’ve embedded a navigation controller to this Does-Nothing-Project🤣. If I press the button of MainViewController, it will instantiate the next ViewController on the far right named ModalViewController. Of course I could’ve created a segue but I just wanted to check it out programatically.

The order of methods being called is quite predictive as shown in the result image below. Then I’ve set breakpoints at loadView, viewDidLoad and viewDidAppear of ModalViewController.swift. I could check that

  • New view controller appears after view is loaded and after the previous view has disappeared.
  • viewWillDisappear is called when ‘Back’ bar button item is pressed.
  • viewDidDisappear of previous view controller comes before viewDidAppear of current view controller.

UIWindow

Prior to moving on to rootViewController having a grasp of UIWindow concept seems necessary. A UIWindow object cannot display visible contents on its own. The role of displaying views are taken by its root view controller. Instead window receive events from UIIKit and forward any relevant events to related views. I’ve captured an image(below) from the view debugger of Does-Nothing-Project and you can see that there’s a UIWindowScene at the bottom.

rootViewController

The root view controller is positioned at the top of the view controller hierarchy. Every window has exactly one root view controller whose content fills the entire window. (Even splitViewController is a root view controller on top of a single window.) In user’s perspective, what one initially sees is the rootViewController.

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/TheViewControllerHierarchy.html#//apple_ref/doc/uid/TP40007457-CH33-SW1

Once I’ve done a project that should display different view controllers depending on user’s log in state. (Let’s not consider ios app rejection guideline for now) If logged-in user should see the main view controller whereas logged-out user should see the log-in page. So I wrote the following code in the SceneDelegate.swift.

There are more to learn and find out about View Controllers. As its concept is important and crucial there are much information and developer documents provided by Apple unlike any other. However not all are well organized in myself so this is where this article will end, hoping to fill this page along the months and years to come.

--

--