问题描述
我无法从另一个类 Menu.swift 调用我的 GameViewController.swift 中的函数.我这样调用函数:
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()
}
}
这是我要调用的函数:
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)
}
}
我的 Menu 类中有一个编译器错误,在 GameViewController.showLeaderboard() 行调用中参数 #1 缺少参数"但我不明白编译器期望什么类型的参数,因为我声明了该函数而无需任何参数.
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.
谢谢
推荐答案
在 GameViewController
中,您将 scoreAction
定义为 instance
方法而不是 class
函数.你应该通过创建GameViewController
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()
}
}
如果你在storyBoard中有GameViewController
,我认为你应该从storyBoard加载GameViewController
I think you should load GameViewController
from storyBoard if you have GameViewController
in storyBoard
这篇关于从另一个类快速调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!