Looking for some simple help as I just learning SWIFT.
I have a sample app with 5 viewcontrollers (it's a gym app) will each viewcontroller focused on a different workout. In each viewcontroller, I want to capture the same data (weight lifted) but for each workout. I am using CoreData which is working great.
I want to use one function (to say capture the weight) and pass in parameters. If I place this function in viewController1 - how do I call it from viewController2?
Thanks (in advance)
-tekgeek
************* my current viewcontroller with function *****************
import UIKit
import CoreData
class Controller1: UIViewController {
@IBOutlet weak var newMaxLiftText: UITextField!
@IBOutlet weak var newMaxLiftLabel: UILabel!
@IBOutlet weak var maxWeightLiftedLabel: UILabel!
@IBAction func newWeightButon(_ sender: UIButton) {
maxWeightLiftedLabel.text = newMaxLiftText.text
updateData()
}
@IBAction func delerteDataBtton(_ sender: UIButton) {
deleteData()
}
@IBAction func loadDataButton(_ sender: UIButton) {
createData()
}
override func viewDidLoad() {
super.viewDidLoad()
}
////////////// Update Function ////////////////
func updateData(){
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "Chest")
fetchRequest.predicate = NSPredicate(format: "name = %@", "Dumbell Chest Press")
do
{
let test = try managedContext.fetch(fetchRequest)
let objectUpdate = test[0] as! NSManagedObject
objectUpdate.setValue(maxWeightLiftedLabel.text!, forKey: "weight")
do{
try managedContext.save()
}
catch
{
print("Error: ",error)
}
}
catch
{
print(error)
}
}