如何在Swift 4中以编程方式向按钮添加IBAction? [英] How do I add a IBAction to a button programmatically in Swift 4?

查看:66
本文介绍了如何在Swift 4中以编程方式向按钮添加IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I add a IBAction to a button programmatically?

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

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

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

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

推荐答案

Swift 4版本:

Swift 4 version:

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

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

这篇关于如何在Swift 4中以编程方式向按钮添加IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆