title: Swift passing calculated data back to previous view controller
body: <p>I am creating my first simple budgeting app. Basically, I take a few user inputs like monthly income &amp; savings goal. Then they click "start", &amp; the app calculates stuff such as, their daily budget etc. </p>
  
  <p>Here I'm running into trouble. After all the calculations, I display "how much you can spend each day" (e.g. $20 a day), which I pass forward through segues from their original inputs on the original screen. </p>
  
  <p>Now, in this VC (<code>UserInfoVC</code>) I created a button which lets them add how much money they spent today. So when they click this "add money spent" button, I open a new VC (<code>AddSubtractMoney</code>) where I present a calculator where they can enter how much they spent today (i.e. $12) and click submit. </p>
  
  <p>I run their input compared to their daily budget to get a New daily budget. </p>
  
  <p>Now, I'm having trouble passing this updated number backwards, to display it on the previous VC on the label "dailySpendingLimitLabel". I know segues are not the best way to go about passing data backwards. </p>
  
  <p>I've tried closures, but I end up getting lost in the syntax, and protocols and delegates (it's my 2nd month coding). </p>
  
  <p>Is there a simple way to achieve passing this data back to the previous VC and populating the data in that previous display label?</p>
  
  <p>Below is the code. </p>
  
  <p>The First snippet is from the UserInfoVC where I display their originally entered data that I segued through. The Second snippet is from the <code>AddSubtractMoney</code> class where I placed the calculator and created an object "newestUpdate" inside a function that allows me to calculate the number they entered on the calculator minus their old daily budget. To arrive at a new budget which I want to present backwards to the <code>UserInfoVC</code>.</p>
  
  <pre><code>class UserInfoViewController : ViewController {
  
  var userNamePassedOver : String?
  var userDailyBudgetPassedOver : Double = 99.0
  var userDailySavingsPassedOver : Double = 778.00
  var userMonthlyEarningsPassedOver : Double?
  var userDesiredSavingsPassedOver : Double?
  var newAmountPassedBack : Double = 0.0
  
  
  @IBOutlet weak var dailySavingsNumberLabel: UILabel!
  @IBOutlet weak var userNameLabel: UILabel!
  @IBOutlet weak var dailySpendingLimitLabel: UILabel!
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      userNameLabel.text = userNamePassedOver
      dailySpendingLimitLabel.text = String(format: "%.2f", userDailyBudgetPassedOver)
      dailySavingsNumberLabel.text = String(format: "%.2f", userDailySavingsPassedOver)
  
  }
  
  
  @IBAction func addSubtractMoneyPressed(_ sender: UIButton) {
  
      performSegue(withIdentifier: "addOrSubtractMoney", sender: self)
  
  
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  
      if segue.identifier == "addOrSubtractMoney"{
  
          let addOrSubtractMoneyVC = segue.destination as! AddSubtractMoney
  
              addOrSubtractMoneyVC.dailyBudgetPassedThrough = userDailyBudgetPassedOver
          }
      }
  
  }
  
  extension UserInfoViewController: AddSubtractMoneyDelegate {
  
  func calculatedValue(value: Double) {
  
      dailySpendingLimitLabel.text = String(userDailyBudgetPassedOver - value)
  
  }
  
  }
  
  import UIKit
  
  protocol AddSubtractMoneyDelegate {
    func calculatedValue(value: Double)
  }
  
  class AddSubtractMoney: UIViewController {
  
  @IBOutlet weak var outputLabel: UILabel!
  
  var runningNumber = ""
  var finalNumberPassedOver : Double?
  var amountPassedBackToUserInfo : Double = 0.0
  var dailyBudgetPassedThrough : Double = 0.0
  
  var delegate: AddSubtractMoneyDelegate?
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      outputLabel.text = "0"
  
  
      // Do any additional setup after loading the view.
  }
  
  
  @IBAction func buttonPressed(_ sender: UIButton) {
      runningNumber += "\(sender.tag)"
      outputLabel.text = runningNumber
  
  
  }
  
  @IBAction func submitNewInfo(_ sender: UIButton) {
  
      // FIX FIX
  
      AddSubtractMoneyController.addToMoneySpentArray(amountISpent: outputLabel.text!)
  
      sendBackUpdatedNumber()
  
      dismiss(animated: true, completion: nil)
  
  }
  
  
  @IBAction func allClearedPressed(_ sender: UIButton) {
      runningNumber = ""
      outputLabel.text = "0"
  }
  
  // THIS LINE PRODUCES THE CORRECT INPUT IN OUTPUT CONSOLE WHEN I PRINT- BUT I CANT FIGURE HOW TO TRANSFER IT BACK TO PREVIOUS VC
  
  func sendBackUpdatedNumber(){
  
      let newestUpdate = UserInfo(whatYouSpentToday: runningNumber, oldDailyBudgetPassed: dailyBudgetPassedThrough)
  
     amountPassedBackToUserInfo = dailyBudgetPassedThrough - Double(runningNumber)!
     newestUpdate.goalToSaveDaily = amountPassedBackToUserInfo
  
      print(amountPassedBackToUserInfo)
  
      self.delegate?.calculatedValue(value: amountPassedBackToUserInfo)
  
  
  }
  
  
  }
  </code></pre>