0

I am creating an application where the user adds a person on the application and after that it right the amount of money they owe that person or that person owes him/her. So while coding I had a set of questions so it would be really helpful if you can help me.

Question 1: I have used UISwitch to know if user owes or the other person owes. So, depending on switch on and off the amount will have negative value if user owes and positive if other person owes. Also, the value is shown on other viewcontroller so how do I pass the state of UISwitch and how do I make the value negative and positive depending on the UISwitch's result?

Question 2: I want to show the balance(So the owes of user to that person would sum up and display) of the user in the title of Navigation Controller so how do it?

ViewControllers: NewOwedDetailViewController: This is where I store new owe details of the person

Code:

import UIKit

class NewOwedDetailViewController: UIViewController {

    @IBOutlet weak var titleTextField: UITextField!
    @IBOutlet weak var locationTextField: UITextField!
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var datePicker: UIDatePicker!

    var person: People? 
    var owe: Owe?

    override func viewDidLoad() {
        super.viewDidLoad()

        titleTextField.delegate = self as UITextFieldDelegate
        locationTextField.delegate = self as UITextFieldDelegate
        amountTextField.delegate = self as UITextFieldDelegate

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        titleTextField.resignFirstResponder()
        locationTextField.resignFirstResponder()
        amountTextField.resignFirstResponder()
    }


    @IBAction func saveOwe(_ sender: Any) {
        let name = titleTextField.text
        let location = locationTextField.text
        let amountText = amountTextField.text ?? ""
        let amount = Double(amountText) ?? 0.0
        let date = datePicker.date

        if let owe = Owe(name: name, location: location, amount: amount, date: date) {
            person?.addToRawOwes(owe)

        //Below try function code has save() as throw function so whenever throw function are there you just add try at the beggining of the function
            do {
                try owe.managedObjectContext?.save()

                self.navigationController?.popViewController(animated: true)
            } catch {
                print("Owe details could not be created")
            }
        }
    }


    @IBAction func oweSwitch(_ sender: UISwitch) {

        if sender.isOn {
            owe?.amount = (owe?.amount)! * (-1)
        } else {
            owe?.amount = (owe?.amount)! * (1)
        }

    }


}

extension NewOwedDetailViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        return true
    }
}

PersonDetailsTableViewController: This is where I display all details of that particular person's owe history

import UIKit
import ChameleonFramework

class PersonDetailsTableViewController: UITableViewController {

    @IBOutlet weak var personDetailsTableView: UITableView!

    let dateFormatter = DateFormatter()

    var person: People?

    override func viewDidLoad() {
        super.viewDidLoad()

        dateFormatter.timeStyle = .long
        dateFormatter.dateStyle = .long
    }

    override func viewWillAppear(_ animated: Bool) {
        self.personDetailsTableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let destination = segue.destination as? NewOwedDetailViewController else {
            return
        }

        destination.person = person

    }

    func deleteOwe(at indexPath: IndexPath) {
        guard let owe = person?.owe?[indexPath.row],
            let managedContext = owe.managedObjectContext else {
                return
        }

        managedContext.delete(owe)

        do {
            try managedContext.save()

            personDetailsTableView.deleteRows(at: [indexPath], with: .automatic)
        } catch {
            print("Could not delete owes")

            personDetailsTableView.reloadRows(at: [indexPath], with: .automatic)
        }

    }
}

extension PersonDetailsTableViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return person?.owe?.count ?? 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = personDetailsTableView.dequeueReusableCell(withIdentifier: "detailsCell", for: indexPath)

        if let owe = person?.owe?[indexPath.row] {
            cell.textLabel?.text = owe.name

            if let amount = person?.owe?[indexPath.row] {
                    cell.detailTextLabel?.text = "₹ \(owe.amount)"
            }
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            deleteOwe(at: indexPath)
        }
    }
}

extension PersonDetailsTableViewController {
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "addOweDetails", sender: self)
    }
}

Screen PersonDetailsTableViewController NewOwedDetailViewController

John Shah
  • 11
  • 3

0 Answers0