我正在尝试为我的按钮设置监听器功能,但我不断收到错误消息。这是如何做到的:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Create the BackGround
self.view.backgroundColor = UIColor.black
let startButton = UIButton()
startButton.setTitle( " start ", for: UIControlState.normal)
startButton.setTitleColor(UIColor.blue, for: UIControlState.normal)
startButton.frame = CGRect(x: 10, y:40 , width: 250, height: 25)
self.view.addSubview(startButton)
startButton.addTarget(self, action: "buttonPressed:" , for: .touchUpInside)
}
func buttonPressed(sender: UIButton!) {
print("hello")
}
}
有谁知道我在哪里犯了错误?
你很亲密。只需替换
startButton.addTarget(self, action: "buttonPressed:" , for: .touchUpInside)
用这个:
startButton.addTarget(self, action: #selector(ViewController.buttonPressed(sender) , for: .touchUpInside)
关于ios - 如何为在 swift 3 中以编程方式定义的按钮设置监听器功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961299/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |