0

I am trying to create an instance of a "budget" class when I click a button that for now just has the information on the inputed gross income. When this button is pressed I want to create an instance of this budget class and then use this instance in a new view controller that appears to display data. What is the best way to do this? Should I be doing this a different way?

Right now I have a budget class as shown below:

import Foundation

class Budget {
    private var _grossIncome: Int!

    var grossIncome: Int {
        return _grossIncome
    }

    init(grossIncome: Int) {
        self._grossIncome = grossIncome
    }
}

And a view controller with the button to be pressed here:

import UIKit

class CreateNewBudgetVC: UIViewController {
    @IBOutlet weak var grossIncomeVal: UITextField!
    @IBOutlet weak var locationVal: UITextField!
    @IBOutlet weak var federalExptVal: UITextField!
    @IBOutlet weak var stateExptVal: UITextField!
    @IBOutlet weak var deductionsList: UITableView!

    @IBAction func cancelPressed(_ sender: Any) {
        performSegue(withIdentifier: "CancelSegue", sender: self)
    }

    @IBAction func generateBudgetPressed(_ sender: Any) {
        performSegue(withIdentifier: "GenerateSegue", sender: self)
        let budget = Budget(grossIncome: Int(grossIncomeVal.text!)!)
    }
}

So, how do I make it so when the "genereateBudgetPressed" method is called on button click it creates a new instance of the budget class with the inputed gross income and I can use this information to display things on a new view controller? Is there a better way to be doing this whole process?

1
  • 1
    There is no reason for _grossIncome to be an Int!. It can just be an Int Commented May 15, 2018 at 20:52

1 Answer 1

1

One way to do this, would be to pass the budget instance as the sender to the performSegue call. That way, in prepare for segue, you can get that value out and populate the segue.destination viewcontroller.

@IBAction func generateBudgetPressed(_ sender: Any) {
    let budget = Budget(grossIncome: Int(grossIncomeVal.text!)!)
    performSegue(withIdentifier: "GenerateSegue", sender: budget)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let budget = sender as? Budget, let destination = segue.destination as? YourDestinationViewController {
        destination.budget = budget
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome thank you, only question is what would the code in the new view controller be? I tried setting up an init() but it is giving me issues.
Well, if you are using storyboards, you generally don't override the init for UIViewControllers. I would declare the UIViewController class with a var budget: Budget! - this is an implicitly unwrapped optional, meaning that you don't have to check for nil, but in the case that the previous controller didn't provide a value in prepare(for:sender:), it will crash your app. This is generally caught in development, but you should decide how you want to handle it - it could also be a normal optional like so: var budget: Budget?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.