Im having trouble calling a function in my GameViewController.swift from another class, Menu.swift. I call the function like this:
class Menu: SKnode { func scoreAction(sender:UIButton!) { self.buttonPlay.removeFromSuperview() self.buttonScore.removeFromSuperview() // CALLING FUNCTION GameViewController.showLeaderboard() } }
And here is the function I'm trying to call:
class GameViewController: UIViewController, UITextFieldDelegate, GKGameCenterControllerDelegate { func showLeaderboard() { var gcViewController: GKGameCenterViewController = GKGameCenterViewController() gcViewController.gameCenterDelegate = self gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards gcViewController.leaderboardIdentifier = "yourleaderboardid" self.presentViewController(gcViewController, animated: true, completion: nil) }
}
I have a Compiler Error inside my Menu class in the line GameViewController.showLeaderboard() "Missing argument for parameter #1 in call" But I don't understand what type of argument the compiler is expecting because I declared the function without any needing any parameters.
Thank you
Languages that support first class functions enable you to use functions and methods just like any other object or value. You can pass them as arguments, save them in properties or return them from another function. In order words, the language treats functions as "first class citizens".
In GameViewController
you have defined scoreAction
as instance
method not the class
function.You should call scoreAction
by making instance of GameViewController
class Menu: SKnode { func scoreAction(sender:UIButton!) { self.buttonPlay.removeFromSuperview() self.buttonScore.removeFromSuperview() // CALLING FUNCTION //see () on GameViewController GameViewController().showLeaderboard() } }
I think you should load GameViewController
from storyBoard if you have GameViewController
in storyBoard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With