我正在尝试使用此代码在iPhone 6模拟器中按下按钮时打印一些文本。但是,我在控制台中没有得到任何结果,我试图从初始化中调用该函数 - 它返回到控制台 - 所以我不知道什么是错的。此外,ViewController中还有其他按钮似乎工作得很好。谢谢你的帮助。
import UIKit
class RatingControl: UIView {
// MARK: Initialisation
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// simple button to test
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
addSubview(button)
}
// MARK: Button Action
func ratingButtonTapped(button: UIButton) {
print("Button pressed ")
}
}