3

I have a function that I'd like to call when a button is pressed, but unlike anything I've done to date, I'd like to be able to reach it from any of several ViewControllers. I don't want to have to duplicate the same chunk of code in each ViewController.

I've tried defining the function I want to call outside of the ViewController class, but I can't figure out what I need to set the target to be in order to reach it. Typically self is used because the function is defined within the class, but what do you do if the function in another file and/or outside of a class?

button.addTarget(???, action: "myFunction:", forControlEvents: UIControlEvents.TouchUpInside)

Edit: Another solution I would be able to use would be to do something like this and define the function I want to call inside the function that creates the button, but I run into the same problem of not knowing what the target needs to be to reach it.

class ViewController {
    // Main code here
}

func makeButton() {
   //  Code to make button here
   button.addTarget(???, action: "buttonPressed",...)

   func buttonPressed() {
       // Code that runs when the button is pressed goes here
   }
}

Thanks in advance!

Erik
  • 2,299
  • 4
  • 18
  • 23

3 Answers3

6

You can find here a complete tutorial. https://github.com/fatihyildizhan/SingletonFunctionForAddTarget

if you don't want do download it:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var btnGotoVacation: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        btnGotoVacation.addTarget(MySingletonClass.sharedInstance, action: Selector("visitBodrum:"), forControlEvents:.TouchUpInside)
    }
}

class MySingletonClass:NSObject {
    static let sharedInstance = MySingletonClass()

    func visitBodrum(sender:AnyObject) {
        print("don't forget to visit yalikavak :) ")
    }
}

The key point is that you should inherit your singleton class from NSObject.

if not you'll probably get this error: enter image description here

and if you do:

enter image description here

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
3

You would usually create another class and implement your code there, then create (and keep) an instance of the class and set it as the target.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks for your answer. I tried doing what you suggested, and I was able to call functions within the new class from an instance I created from viewDidLoad, and I was able to set it as the target for the button, but when I run it in the simulator and press the button the app crashes and doesn't even give me an error code or crash report in the console. Do you have any idea why that would be happening? – Erik Jan 09 '15 at 15:36
  • You're right. Moving the declaration up a level fixed it! Thanks so much! – Erik Jan 09 '15 at 17:08
2

You would have to pass a reference of the class that contains the function. For example, if you are using a singleton pattern:

button.addTarget(FunctionEngine.sharedInstance, action: "buttonFunction", ...
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Would you mind elaborating on what `sharedInstance` is? I presumed it was a function, but when I tried to implement your solution by creating a class called FunctionEngine with a method called sharedInstance, I got an error because the target has to be an object. – Erik Jan 09 '15 at 15:31
  • You have to implement it yourself e.g. like [this](http://stackoverflow.com/a/24073016/427083). It thought you know the [Singleton Pattern](https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html). – Mundi Jan 09 '15 at 20:26