Reputation: 21
Hi all, I am trying to build an app for my school project that I am building on Xcode for iOS. The app involves the user being able to save information about their bills. This data is string data and includes the potential for the user being able to save the name of a bill and when the bill was received. I want it that if the bill is related to a specific expenses, for that data to be saved to a specific record that holds data for that specific expense. For example, if the user received a bill for their car, they would enter the bill details and that data could be saved to one specific record that holds all car expense bill data. The way the user could specify where they want to data to go too is through switching on the relevant switch. I am not sure how I can get it that when the user turns on, for example car, that the data will save to that record. Could you please help? I have included my code and the photo of my interface.
Thank you
class ViewControllerAdd: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cars.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//ensure the cell identifier has been labelled "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let cars = cars[indexPath.row];
let text = "\(cars.provider) \(cars.date)";
cell.textLabel?.text=text
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tblCar.delegate = self
tblCar.dataSource = self
tblCar.reloadData()
// Formatting for take picture button
imgView.backgroundColor = .secondarySystemBackground
btnLook.backgroundColor = .systemBlue
btnLook.setTitle("Take Picture", for: .normal)
btnLook.setTitleColor(.white, for: .normal)
// datepicker function from https://www.youtube.com/watch?v=fhRJ5HQjBIE
// This is how the date picker changes the date and date is saved
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
datePicker.addTarget(self, action: #selector(dateChange(datePicker:)), for: UIControl.Event.valueChanged)
datePicker.frame.size = CGSize(width: 0, height: 300)
datePicker.preferredDatePickerStyle = .wheels
txtDate.inputView = datePicker
txtDate.text = formateDate(date: Date())
// Getting keyboard to retract
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
//outlet
// Make segmented control action
@IBOutlet weak var txtBill: UITextField!
@IBOutlet weak var txtDate: UITextField!
@IBOutlet weak var btnLook: UIButton!
@IBOutlet weak var swiCar: UISwitch!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var tblCar: UITableView!
struct Car{
let provider: String
let date: String
// let bill: String
}
struct Went{
let provider: String
let date: String
// let bill: String
}
struct South{
let provider: String
let date: String
// let bill: String
}
struct Bruns{
let provider: String
let date: String
// let bill: String
}
struct Home{
let provider: String
let date: String
// let bill: String
}
struct Telephone{
let provider: String
let date: String
// let bill: String
}
var cars: [Car] = []
var went: [Went] = []
var south: [South] = []
var bruns: [Bruns] = []
var home: [Home] = []
var telephone: [Telephone] = []
//action
@IBAction func btnAdd(_ sender: Any) {
let newCar = Car(provider: txtBill.text!, date:txtDate.text!)
let newWent = Went(provider: txtBill.text!, date: txtDate.text!)
let newBruns = Bruns(provider: txtBill.text!, date: txtDate.text!)
let newSouth = South(provider: txtBill.text!, date: txtDate.text!)
let newHome = Home(provider: txtBill.text!, date: txtDate.text!)
let newTelephone = Telephone(provider: txtBill.text!, date: txtDate.text!)
cars.append(newCar)
went.append(newWent)
south.append(newSouth)
bruns.append(newBruns)
home.append(newHome)
telephone.append(newTelephone)
let imageData = imgView.image!.pngData()
let compressedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
let alert = UIAlertController(title: "Saved", message: "Image Saved", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style:.default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion:nil)
if swiCar.isOn{
cars.append(newCar) = tblCar
}
}
// Functionality
@IBAction func btnPhoto(_ sender: Any) {
// Checks if app can access camera
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
// If camera can be accessed, it will set the class to the delegate and control the camera
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
// Allows user to edit photo
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
@objc func dateChange(datePicker: UIDatePicker) {
txtDate.text = formateDate(date: datePicker.date)
}
func formateDate(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd yyyy"
return formatter.string(from: date)
}
}
Upvotes: 0
Views: 67
Reputation: 47
If you want to segregate expenses based on type of expenses, have enum
of "expenseType" something like below.
enum ExpenseType{
case Car
case Telephone
case Grossary
case Misc
}
Have a single expense object in which mention "expenseType" as attribute. Based on user selection of switch add that expense to corresponding record. In this case you should not allow user to select more than one switch.
Upvotes: 0