我目前正在使用Apple的Swift教程。在其中,我试图使自定义按钮响应点击,然后打印到控制台。似乎根本没有响应任何水龙头,或者至少不会打印它对控制台的响应。
代码如下,如果需要,请告诉我。
import UIKit
class RatingControl: UIView {
//MARK: Initializaion
required init?(coder aDecoder: NSCoder) { // overrides it's superclass implementation of the initializer
super.init(coder: aDecoder)
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)
func intrensicContentSize() -> CGSize {
return CGSize(width: 240, height: 44)
}
}
//MARK: Button Action
func ratingButtonTapped(button:UIButton) {
print("will it print?")
}
}
答案 0 :(得分:0)
请尝试.TouchUpInside而不是TouchDown
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchUpInside)