一尘不染

从另一个类迅速调用函数

swift

我无法从另一个类Menu.swift调用GameViewController.swift中的函数。我这样调用该函数:

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)
    }

}

我在菜单类内的GameViewController.showLeaderboard()行中出现编译器错误“调用中的参数#1缺少参数”,但我不理解编译器期望的参数类型,因为我不需要任何声明就声明了该函数参数。

谢谢


阅读 229

收藏
2020-07-07

共1个答案

一尘不染

GameViewController您定义scoreActioninstance方法而不是class函数的scoreAction情况下,应通过使GameViewController

class Menu: SKnode {

    func scoreAction(sender:UIButton!) { 
        self.buttonPlay.removeFromSuperview()
        self.buttonScore.removeFromSuperview()
         // CALLING FUNCTION 
         //see () on GameViewController
        GameViewController().showLeaderboard()    
     }
}

我认为GameViewController如果您GameViewController在storyBoard中,则应该从storyBoard 加载

2020-07-07