Call function from FirstViewController to SecondViewController using Swift Programming Language.

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

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.