David Seek
David Seek

Reputation: 17132

How to make a method available in the whole project

I have the following code to handle a swipe gesture.

    func handleSwipes(sender:UISwipeGestureRecognizer) {

    if (sender.direction == .Right) {
        print("Swipe Right")
        self.performSegueWithIdentifier("eventsModally", sender: self)
    }
}

What is the best practise to make this method available in the whole project, instead of implementing it in every ViewController class?

Help is very appreciated.

Upvotes: 4

Views: 66

Answers (1)

Tamás Zahola
Tamás Zahola

Reputation: 9319

Put it in a class extension.

extension UIViewController {
    func handleSwipes(sender:UISwipeGestureRecognizer) {
        if (sender.direction == .Right) {
            print("Swipe Right")
            self.performSegueWithIdentifier("eventsModally", sender: self)
        }
    }
}

Upvotes: 6

Related Questions