Question: I have a class where written is a function creating my button:

LoginButton.swift

```
func createButton() {
    let myButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(Foo().buttonPressed(_:)), for: .touchUpInside)

    }()
}

```

Now in my second class, Foo.swift, I have a function that just prints a statement

Foo.swift

```
@objc func buttonPressed(_ sender: UIButton) {
    print("button was pressed")
}

```

When ran I get no errors except when I try to press the button, nothing happens. Nothing prints, the `UIButton` doesn't react in any way. Really not sure where the error occurs because Xcode isn't printing out any type of error or warning message.

Answer: 
The action method is called in the target object. Thus, you have either to move `buttonPressed` to the class which contains `createButton` or to pass an instance of `Foo` as a target object.

But note that a button is not the owner of its targets. So, if you just write:

```
button.addTarget(Foo(), action: #selector(buttonPressed(_:)), for: .touchUpInside)

```

This will not work, because the `Foo` object is immediately released after that line. You must have a strong reference (e.g. a property) to `Foo()` like

```
let foo = Foo()

func createButton() {
    let myButton: UIButton = {
        let button = UIButton()
        button.addTarget(foo, action: #selector(buttonPressed(_:)), for: .touchUpInside)

    }()
}

```
Just pass `Selector` as function argument.

```
func createButtonWith(selector: Selector) {

    let myButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: selector), for: .touchUpInside)

    }()

}

```

And call this function like below...

```
createButtonWith(selector: #selector(Foo().buttonPressed(_:)))

```