43

I'm 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 needing any parameters.

1
  • You are trying to call showLeaderboard like a class method, but it is an instance method. You need to instantiate an instance of GameViewController and then you can call showLeaderboard. Commented Sep 28, 2014 at 5:55

2 Answers 2

69

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

Sign up to request clarification or add additional context in comments.

1 Comment

Why does this have so many upvotes? This is wrong. You end up calling showLeaderboard() on a throwaway instance of GameViewController. Sure, it fixes the compiler error but at runtime, nothing is going to work as expected.
12

If you want to perform any method on action in another class, you have to use 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() //2nd initialiser
obj.delegate = self

// if you want to use this scenario with table view cell in cellForRowAt
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell else { return UITableViewCell() }
cell.delegate = self

Hope this helps.

2 Comments

Can you please explain what 2nd initialiser inside let obj = 2nd initialiser is?
let obj = 2nd initialiser Here obj is the object of 2nd initialiser like:- let cell = ?*(tableView.dequeueReusableCell(withIdentifier: "TVCSelectProfessionalForConsultation") as? TVCSelectProfessionalForConsultation) @ClassA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.