My app is a loan calculator that calculates 1. Monthly Payment, 2. Total interest, 3. Total amount.
There is another button that after clicked I want it to show the second view controller with a table view of the payout structure for each month.
I can do this with a For in loop in the same view controller but I want it to display the payout structure in the second view controller.
I know that I will have to write a function with a For in loop and put it in viewDidLoad() to get the table to load when this view controller loads.
I need to pass the result data to the second view controller to get this to happen.
This is my first view controllers code that I want to pass to the second view controller. The first view controllers class is named ViewController. The second view controllers class is named PayoutStructure.
else if txtCost.text != nil && txtDownPayment.text != nil && txtMonths.text != nil && txtAPR.text != nil {
// Clear the message.
lblMessage.text = ""
dblCost = Double(txtCost.text!)!
dblDownPayment = Double(txtDownPayment.text!)!
intMonths = Int(txtMonths.text!)!
dblAPR = Double(txtAPR.text!)!
// Get the tax rate.
dblTaxRate = dblTaxRate / 100
// Get the APR rate.
dblAPR = dblAPR / 100
// Get the monthly rate.
dblMonthlyRate = dblAPR / dblMONTHS_YEAR
// Get the total tax.
dblTotalTax = (dblCost) * (dblTaxRate)
// Get the monthly payment.
dblMonthlyPayment = ((dblCost + dblTotalTax) - dblDownPayment) * (dblMonthlyRate / (1 - pow(1 + dblMonthlyRate, Double(-intMonths))))
// Get the amount of the loan.
dblLoan = dblCost + dblTotalTax - dblDownPayment
// Get the total interest.
dblTotalInterest = dblMonthlyPayment * Double(intMonths) - dblLoan
// Get the Total Amount.
dblTotalAmount = dblCost + dblTotalInterest + dblTotalTax
// Get the Vehicle Cost.
dblCost = ((dblTotalAmount - dblTotalInterest) - dblTotalTax)
// Format the results.
let fCost = NSNumber(value: dblCost)
let fResultCost = NumberFormatter.localizedString(from: fCost, number: .currency)
let fDownPayment = NSNumber(value: dblDownPayment)
let fResultDownPayment = NumberFormatter.localizedString(from: fDownPayment, number: .currency)
let fLoan = NSNumber(value: dblLoan)
let fResultLoan = NumberFormatter.localizedString(from: fLoan, number: .currency)
let fMonthlyPayment = NSNumber(value: dblMonthlyPayment)
let fResultMonthkyPayment = NumberFormatter.localizedString(from: fMonthlyPayment, number: .currency)
let fAPR = NSNumber(value: dblAPR)
let fResultAPR = NumberFormatter.localizedString(from: fAPR, number: .percent)
let ftaxRate = NSNumber(value: dblTaxRate)
let fResultTaxRate = NumberFormatter.localizedString(from: ftaxRate, number: .percent)
let fTotalInterest = NSNumber(value: dblTotalInterest)
let fResultTotalInterest = NumberFormatter.localizedString(from: fTotalInterest, number: .currency)
let fTotalTax = NSNumber(value: dblTotalTax)
let fResultTotalTax = NumberFormatter.localizedString(from: fTotalTax, number: .currency)
let fTotalAmount = NSNumber(value: dblTotalAmount)
let fResultTotalAmount = NumberFormatter.localizedString(from: fTotalAmount, number: .currency)
txtViewLabels.text = " APR:\n Tax%:\n Months:\n Cost:\n Total Tax:\n Down Payment:\n Loan Amount\n Monthly Payment:\n Total Interest:\n Total Amount:\n"
txtViewResults.text = " \(fResultAPR)\n \(fResultTaxRate)\n \(intMonths)\n \(fResultCost)\n \(fResultTotalTax)\n \(fResultDownPayment)\n \(fResultLoan)\n \(fResultMonthkyPayment)\n \(fResultTotalInterest)\n \(fResultTotalAmount)"
}
}
My second view controllers code is:
class PayoutStructure: UIViewController, UITableViewDataSource, UITableViewDelegate {
var passedData = ViewController()
var data: [String] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
return cell
}
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}