Learning w/ Swift

Basic setup of a UIViewController in UIKit

View Controllers in UIKit can best be described as containing one screen worth of information. Data, functions, UI - whatever the screen needs to be complete.

When we create a new view controller it contains the following minimal code to get the screen up and running, and allows us to easily customize our screen how we want it from there.

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
}

The first part of the code says that we want to create a new class named ViewController, that conforms to UIViewController, which is Apple’s default screen type in UIKit. This initial setup will create a blank white screen, which can later be customized with data and UI elements.

Before anything will show up on screen, we need to call the method viewDidLoad(), which will load up our view and any data associated with it if it hasn’t been loaded into memory yet.

We can change Apple’s default behavior from UIViewController by using the override keyword, and using super.viewDidLoad() underneath it to execute the UIViewController code before running anything else.

Anything under super.viewDidLoad() can be used to furnish the rest of the screen with all of the information it needs.