Here is the situation in your Xcode project where you want to call a method from another class Swift file to your original class of Swift file.
Let’s call your original swift class file ‘FirstViewController’, which has ‘UIViewController’ as a superclass.
The second swift that you want to call a function from, we will assume that it has ‘SecondViewController’ as the class name & it has ‘UIViewController’ as a superclass.
You have written a function called ‘print something()’ in your ‘SecondViewController’ that would be triggered when somebody taps on the button present on ‘FirstViewController’ as IBAction called ‘print this(sender: UIButton)’
So, you will write a print function as follows:
SecondViewController().printSomething() // ViewControllerClassName().FunctionName()
So your ‘SecondViewController’ would look like this:
class SecondViewController: UIViewController {
func printSomething() {
print("printSomething function has been called")
}
}
So your ‘FirstViewController’ would look like this:
class FirstViewController: UIViewController {
@IBAction func printThis(sender: UIButton) {
SecondViewController().printSomething() // ViewControllerClassName().FunctionName()
}
}
Hope it helps,
Thanks & Regards
Mandar Apte