4.2. Patching the View class with the ViewController
Now that we are done with the FirstScreenView file, we have to initialize the view in the ViewController. So open the ViewController.swift file. Let's create an instance of the FirstScreenView:
//
// ViewController.swift
// App4
//
import UIKit
class ViewController: UIViewController {
//MARK: initializing the First Screen View...
let firstScreen = FirstScreenView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}So, firstScreen is the instance of the View, FIrstScreenView. Now the view of the ViewController should be firstScreen, right? So, we need to define it when the view is loading (not when the view did load). So we can write the following:
//
// ViewController.swift
// App4
//
import UIKit
class ViewController: UIViewController {
//MARK: initializing the First Screen View...
let firstScreen = FirstScreenView()
//MARK: add the view to this controller while the view is loading...
override func loadView() {
view = firstScreen
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}Here we override the loadView() method so that we can patch the view with the controller while the view is loading.
Now, let's run the app:

Adding the PickerView logic
Let's define the moods like the app, App3, which we built previously.
Now let's adopt the protocols UIPickerViewDelegate and UIPickerViewDataSource.
Now, it's time to patch the PickerView's (pickerMood) delegate and data source to the ViewController. Let's add the following to viewDidLoad() method:
Notice that I am calling pickerMood by writing firstScreen.pickerMood since the instance of the View where the PickerView resides is firstScreen.
The whole code so far is as follows:
Now, let's run it.

See, we patched the PickerView with data!
Last updated
Was this helpful?