Im having trouble calling a function in my GameViewController.swift from another class, Menu.swift. I call the function like this:
我在我的GameViewController中调用一个函数有困难。另一个班的斯威夫特。我这样称呼这个函数:
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.
在我的菜单类的line GameViewController.showLeaderboard()中有一个编译错误“在调用中缺少参数#1的参数”,但是我不理解编译器期望的参数类型,因为我声明了函数,不需要任何参数。
Thank you
谢谢你!
2 个解决方案
#1
52
In GameViewController
you have defined scoreAction
as instance
method not the class
function.You should call scoreAction
by making instance of GameViewController
在GameViewController中,你定义了scoreAction作为实例方法,而不是类函数。你应该通过创建GameViewController实例来调用scoreAction
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
我认为你应该从故事板中加载GameViewController如果你在故事板中有GameViewController
#2
3
If you want to perform any method on action in another class, you have to use protocol.
如果您想在另一个类中执行任何操作方法,您必须使用protocol。
//Outside 1st class
/ /外部一级
protocol abc {
func xyz()
}
//Inside 1st class
/ /一级
var delegate: abc?
//Inside 1st class on action
//第一节课
self.delegate.xyz()
//Inside 2nd class you want to perform method
//在二班你要执行方法
extension 2nd: abc {
func xyz(){
//code
}
}
//Inside 2nd class where the instantiate process is performed
//在执行实例化过程的第二类内部
let obj = 2nd initialiser
obj.delegate = self
Hope this helps.
希望这个有帮助。
#1
52
In GameViewController
you have defined scoreAction
as instance
method not the class
function.You should call scoreAction
by making instance of GameViewController
在GameViewController中,你定义了scoreAction作为实例方法,而不是类函数。你应该通过创建GameViewController实例来调用scoreAction
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
我认为你应该从故事板中加载GameViewController如果你在故事板中有GameViewController
#2
3
If you want to perform any method on action in another class, you have to use protocol.
如果您想在另一个类中执行任何操作方法,您必须使用protocol。
//Outside 1st class
/ /外部一级
protocol abc {
func xyz()
}
//Inside 1st class
/ /一级
var delegate: abc?
//Inside 1st class on action
//第一节课
self.delegate.xyz()
//Inside 2nd class you want to perform method
//在二班你要执行方法
extension 2nd: abc {
func xyz(){
//code
}
}
//Inside 2nd class where the instantiate process is performed
//在执行实例化过程的第二类内部
let obj = 2nd initialiser
obj.delegate = self
Hope this helps.
希望这个有帮助。