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: 
You are missing with target. So make instant of target globally and make use of it as target for button action handler.

```
class ViewController: UIViewController {

let foo = Foo()

override func viewDidLoad() {
    super.viewDidLoad()
    createButton()
}

func createButton() {
    let myButton: UIButton = {
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        button.backgroundColor = UIColor.red
        button.setTitle("Tap me", for: .normal)
        button.addTarget(self.foo, action: #selector(self.foo.buttonPressed(_:)), for: .touchUpInside)
        return button
    }()
    myButton.center = self.view.center
    self.view.addSubview(myButton)
}
}

```

Class Foo:

```
class Foo {

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

```
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(_:)))

```