Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xcode - Calling Function from another class 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()    
     }
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...