Monomeeth
Monomeeth

Reputation: 773

Using Swift, how do I call a function coded in one view controller from a button in another view controller?

I've started to learn programming by doing some online courses on the Swift programming language. I'm experimenting with an Xcode project in which I have two view controllers in my main storyboard.

In the project I have a button that takes me from view controller 1 to view controller 2. View controller 2 has three buttons:

The function in question is actually very simple:

func ResetGame()
{
    PlaySoundButton()
    score = 0
    totalquestionsasked = 0

}

I've lost count of all the ways I've tried to get this to work, but originally I thought it would just be a straightforward button with one of the following snippets of code in the 2nd view:

@IBAction func buttonResetGame(sender: UIButton) {

    ResetGame()
}

Or

 @IBAction func buttonResetGame(sender: AnyObject) {

        ResetGame()
    }

But of course this results in a Use of unresolved identifier 'ResetGame' error.

I also tried coding the function directly within the button code as per the following example:

@IBAction func buttonResetGame(sender: UIButton) {

    PlaySoundButton()
    score = 0
    totalquestionsasked = 0

}

But of course that didn't work either.

I've researched this and it appears this is possible in Objective C and other programming languages, but I can't seem to find examples of this using Swift. Does this mean it's not possible, or just that it's not the done thing?

In summary:

Upvotes: 1

Views: 2758

Answers (2)

Lubos
Lubos

Reputation: 1209

In general, it is possible, but not recommended, because a View Controller is there for displaying a view, not for sharing a functionalities across different View Controllers.

You should implement this by creating a separate class to be referenced from the both View Controllers, where you would define this function, which holds the common functionality.

EDITED: If you don't want to share common functionality from 2 view controllers, but you just simply need to call some code from another controller, you can use delegation pattern like

class FirstController: UIViewController, YourProtocol {

     override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {

         if segue?.identifier == "yourSegueName" {
             let viewController = segue!.destinationViewController as! SecondController
             viewController.controller = self
         }
     }

     func someFunction() {

     }
}

class SecondController: UIViewController {

     var controller: YourProtocol?   // reference to the delegate alias First Controller

         override func viewDidLoad() {
              super.viewDidLoad()
              controller.someFunction()   // call function from the delegate alias first controller
    }

}

protocol YourProtocol {

     func someFunction()

}

Upvotes: 1

2ank3th
2ank3th

Reputation: 3109

To call ResetGame() you need instance of 1st view controller then you can call instance.ResetGame(). You can pass the instance of 1st view controller to 2nd View Controller using

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            let secondViewController = segue.destinationViewController as! SecondViewController
            destinationVC.instance = self
        }

Upvotes: 0

Related Questions