Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a IBAction to a button programmatically in Swift 4?

How do I add a IBAction to a button programmatically?

button.addTarget(self, action: Selector(("buttonAction:")), for: 
.touchUpInside)

func buttonAction(sender: Any) {
    print("test")
}

That gives me an "Thread 1: signal SIGABRT" error.

like image 290
Mofawaw Avatar asked Oct 26 '17 08:10

Mofawaw


1 Answers

Swift 4 version:

button.addTarget(self, action: #selector(action(sender:)), for: .touchUpInside)

@objc private func action(sender: Button) {
    print("test")
}
like image 145
Milan Nosáľ Avatar answered Sep 23 '22 03:09

Milan Nosáľ