title: Using Swift, how do I call a function coded in one view controller from a button in another view controller?
body: <p>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.</p>
  
  <p>In the project I have a button that takes me from view controller 1 to view controller 2. View controller 2 has three buttons:</p>
  
  <ul>
  <li>one triggers a new email via the mail app</li>
  <li>one closes the screen and returns the user to the first view controller screen</li>
  <li>and the third one was supposed to call a function (the code for which sits within the ViewController.swift for the first view controller).</li>
  </ul>
  
  <p>The function in question is actually very simple:</p>
  
  <pre><code>func ResetGame()
  {
      PlaySoundButton()
      score = 0
      totalquestionsasked = 0
  
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>@IBAction func buttonResetGame(sender: UIButton) {
  
      ResetGame()
  }
  </code></pre>
  
  <p>Or</p>
  
  <pre><code> @IBAction func buttonResetGame(sender: AnyObject) {
  
          ResetGame()
      }
  </code></pre>
  
  <p>But of course this results in a <em>Use of unresolved identifier 'ResetGame'</em> error.</p>
  
  <p>I also tried coding the function directly within the button code as per the following example:</p>
  
  <pre><code>@IBAction func buttonResetGame(sender: UIButton) {
  
      PlaySoundButton()
      score = 0
      totalquestionsasked = 0
  
  }
  </code></pre>
  
  <p>But of course that didn't work either.</p>
  
  <p>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?</p>
  
  <p>In summary:</p>
  
  <ul>
  <li>Is this possible?</li>
  <li>If so, how?</li>
  <li>If it's not recommended, why not?</li>
  <li>And finally, what's the recommended approach to use a function across view controllers?</li>
  </ul>