我正在创建一个应用程序,用户在应用程序中添加一个人,之后,他们欠该人或该人欠他/她的金额是正确的。因此,虽然编码我有一系列问题所以如果你可以帮助我真的很有帮助。
问题1:我使用UISwitch来了解用户欠款或其他人欠款。因此,根据开启和关闭,如果用户欠款,金额将具有负值,如果其他人欠款,则金额将为正。此外,该值显示在其他viewcontroller上,那么如何传递UISwitch的状态以及如何根据UISwitch的结果使值为负值和正值?
问题2:我想在导航控制器的标题中显示用户的余额(那么用户对该人的欠款总结和显示),那该怎么做呢?
ViewControllers: NewOwedDetailViewController:这是我存储该人的新欠款细节的地方
代码:
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:这是我显示该特定人的历史的所有细节
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)
}
}
屏幕 PersonDetailsTableViewController NewOwedDetailViewController