Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to call a function inside another class? (Swift)

So, I've got a fairly simple question regarding Classes and Functions in Swift.

Let's say I make an app with SpriteKit. I have a GameScene (SKScene), a GameViewController (UIViewController) and a main storyboard for the GameViewController.

If I wanted to have a button on top of my GameScene, I'd connect a button IBOutlet from the main storyboard to the GameViewController. But how can I set it up, so that if I touch the button I can call a function inside the GameScene?

Do I have to handle this with delegation or is there a simpler method?

EDIT:

To elaborate, the function I'd like to call is:

  func resetScene(){
        self.removeAllActions()
        self.removeAllChildren()
    }

I tried to declared as a class func but somehow it doesn't quite work.

like image 470
user3545063 Avatar asked Feb 25 '17 14:02

user3545063


People also ask

Can you call a function from a different class?

Conclusion. In Java, a method can be invoked from another class based on its access modifier. For example, a method created with a public modifier can be called from inside as well as outside of a class/package. The protected method can be invoked from another class using inheritance.

How do you call a function in SwiftUI?

As you can see, you define a function by writing func , then your function name, then open and close parentheses, then a block of code marked by open and close braces. You then call that function by writing its name followed by an open and close parentheses.


2 Answers

There are basically four ways of calling a function from an other class.

  1. Instance of the class. If you have an instance of say class Object, you can call Object's methods over that instance like myObject.buttonTapped().

  2. By delegation: Create a protocol for class A and declare the method in the protocol. Class A must have an instance of the protocol. Call that method in the buttonTapped method like delegate.notifyButtonTapped(). In class B, conform to the protocol and implement the notifyButtonTapped() method.

  3. Notification: Post a notification over NotificationCenter from class A and addObserver to in class B for that notification. Check this link for further information.

  4. Closure/Blocks: In this case closure is not the best solution and they can be fairly complex to fully understand

You can go with the delegation here. Check this link from rw for clarification.

like image 50
Pink Avatar answered Sep 20 '22 23:09

Pink


I don't like the way how you reset the SKScene, usually you can present the same scene to have a really cleaned scene, but I don't know your code so maybe I'm wrong.

About to how to access to the viewController of your scene you could find many ways as for example the protocol/delegate method but another one of them could be:

extension UIView {
    func getViewController<T: UIViewController>() -> T? {
        var responder = self.next
        while responder != nil {
            if let viewController = responder as? T {
                return viewController
            }
            responder = responder!.next
        }
        return nil
    }
}

And to use it :

if let gameVC = self.view?.getViewController(), gameVC is GameViewController {
    let vc = gameVC as! GameViewController
    print("That's all folks \(vc)")
}
like image 45
Alessandro Ornano Avatar answered Sep 20 '22 23:09

Alessandro Ornano