如何在Swift 4中以编程方式向按钮添加IBAction?

Mof*_*waw 3 xcode uibutton ios swift

如何以编程方式将IBAction添加到按钮?

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

func buttonAction(sender: Any) {
    print("test")
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个" 线程1:信号SIGABRT "错误.

Mil*_*sáľ 8

Swift 4版本:

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

@objc fileprivate func action(sender: Button) {
    print("test")
}
Run Code Online (Sandbox Code Playgroud)