5.6. Second screen, part 2: Setting up Add Expense View Controller
Now, let's open the AddExpenseViewController.swift file. We need to patch this view controller with the front-end view (AddExpenseView) we created. So, we write the following code to load the view:
//
// AddExpenseViewController.swift
// App5
//
// Created by Sakib Miazi on 5/18/23.
//
import UIKit
class AddExpenseViewController: UIViewController {
//MARK: initializing the ADDExpenseView...
let addExpenseScreen = AddExpenseView()
//MARK: set the current view to addExpenseScreen...
override func loadView() {
view = addExpenseScreen
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Patching the PickerView to pick the type of expense
Now, let's patch the PickerView (addExpenseScreen.pickerType) to the controller.
We will pick the type of expense using this PickerView. We know that we added an array of four expense types in ViewController.swift. Since we need to use the same array instead of writing the array again, we can define a static array to be shared with all the classes in the project. The keyword static makes it persistent in the memory while the app is running. Do not make all the data static; you can keep it static if it is small shared data.
Defining static array 'types'
Let's create a new Swift file named "Utilities.swift" in the project.

Let's add the following code to the new file:
Here, the Utilities class holds these usable shared data like the types array. We kept the types array as static.
Now let's get back to implementing the PickerView in AddExpenseViewController.swift file.
Adopting the Picker View's protocols
So, we again use the extension keyword to adopt UIPickerViewDelegate and UIPickerViewDataSource protocols. Let's write the code below to AddExpenseViewController.swift file:
We have only one component to select in this PickerVIew, so the numberOfComponents should return just 1. We are displaying the Utilities.types array, so numberOfRowsInComponent should return Utilities.types.count. We keep a variable selectedType to keep the current selection of the user. So, we set the currently selected row to selectedType variable in titleForRow and return Utilities.types[row].
Now, we need to patch the delegate and data sources of the PickerView with AddExpenseViewController. Add the following couple of lines in viewDidLoad() method:
Now, we need to update ViewController.swift file to be able to navigate to AddExpenseViewController.swift and get back with the new expense. So, let's add an instance variable of ViewController named "delegate" to AddExpenseViewController.
Housekeeping: Updating Navigation controller and patching Utilities.types in View Controller
Open the ViewController.swift file. Now, let's complete the plus (+) button actions. When we tap on the + button on the Navigation Bar, we need to switch from the First Screen to Add Expense Screen. So let's write the following code in the method @objc func onAddBarButtonTapped():
The code above creates a new Add Expense Screen, sets the delegate, and pushes it on the Navigation Stack. Now let's run the app:

Updating ViewController to use Utilities.types instead of the local array.
Now let's remove the array types from ViewController.swift:
Also, we can now remove the dummy data from the codes. So remove the following lines from ViewController's viewDidLoad() method:
So far, the updated controller codes are as follows:
ViewController.swift
AddExpenseViewController.swift
Last updated
Was this helpful?