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.

Image for post

https://developer.apple.com/documentation/uikit/uiviewcontroller

Image for post

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.

#view-controller #ios-app-development #swift #ios #apple

UIViewControllers Flashback
1.25 GEEK