5.7. Second screen, part 3: Send new expense back to ViewController and update the TableView
//
// AddExpenseViewController.swift
// App5
//
// Created by Sakib Miazi on 5/18/23.
//
import UIKit
class AddExpenseViewController: UIViewController {
//codes omitted...
override func viewDidLoad() {
//codes omitted...
//MARK: adding the action for tapping on buttonAdd...
addExpenseScreen.buttonAdd.addTarget(self, action: #selector(onAddButtonTapped), for: .touchUpInside)
}
//MARK: action for tapping buttonAdd..
@objc func onAddButtonTapped(){
var title:String?
if let titleText = addExpenseScreen.textFieldTitle.text{
if !titleText.isEmpty{
title = titleText
}else{
//do your thing to alert user...
return
}
}
var amount = 0.0
if let amountText = addExpenseScreen.textFieldAmount.text{
if !amountText.isEmpty{
if let optionalAmount = Double(amountText){
amount = optionalAmount
}else{
//alert the user that it's not a valid input...
return
}
}else{
//do your thing to alert the user...
return
}
}
let newExpense = Expense(title: title, amount: amount, type: selectedType)
delegate.delegateOnAddExpense(expense: newExpense)
navigationController?.popViewController(animated: true)
}
}
//codes omitted...(Handling Double inputs)
Delegating the task to ViewController

Previous5.6. Second screen, part 2: Setting up Add Expense View ControllerNext5.8. Tapping a cell in TableView and Practice exercise
Last updated