I have two ViewControllers, FirstViewController and SecondViewController. Both have an own Swift file, FirstViewController.swift and
You may use NSNotificationCentre to accomplish this task.
In viewDidLoad method of your SecondViewController class register self as observer to receive notification broadcasts:-
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(showAlert), name: NSNotification.Name(rawValue: "callForAlert"), object: nil)
}
and in FirstViewController's button action method you should fire the notification by writing :-
@IBAction func callFunctionInOtherClass(sender: AnyObject) {
//Call "func showAlert" in SecondViewController when clicking the UIButton in FirstViewController
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "callForAlert"), object: nil)
}
Don't forget to call removeObserver in SecondViewController's viewDidUnload method.