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!