Calling a func from one ViewController to another ViewController

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

NotificationCenter or Delegate

Thanks - which one is easier and ya got any examples?

import UIKit

class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(getSome(noti:)), name: Notification.Name.Action.CallVC1Method, object: nil)
    }
    
    //get value
    @objc func getSome(noti: Notification){
        guard let dic = noti.object as? Dictionary<String, String>, let date = dic["date"]  else {
            return
        }
        print(date) //you can get `"2019-1-2"` here
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

class ViewController2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    @objc func buttonClick() {
        //send value
        NotificationCenter.default.post(name: Notification.Name.Action.CallVC1Method, object: ["date": "2019-1-2"])
    }
}

extension Notification.Name {
    struct Action {
        //notification name
        static let CallVC1Method = Notification.Name("CallVC1Method")
    }
}

Thanks. Here is what I am working with: Below is my Controller1 with the function updateData which I want to use in other controllers. The yellow highlighted part was added from your example but I am not sure this is correct as I want to call this function inline (not on a button click). What oud my controller that makes the call look like?

I really do appreciate the help - I am learning!

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()

NotificationCenter.default.addObserver(self, selector: #selector(updateData(noti:)), name:Notification.Name.Action.CallVC1Method, object: nil)

}

////////////// 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)

    }

}

I think you should take a look at a series of tutorials. It doesn't need much.