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

时间:2017-10-26 08:59:52

标签: ios swift xcode uibutton

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

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

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

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

1 个答案:

答案 0 :(得分:7)

Swift 4版本:

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

@objc fileprivate func action(sender: Button) {
    print("test")
}