2016-09-19 28 views
0

我目前正在通過蘋果的Swift教程。其中,我試圖讓一個自定義按鈕響應輕按,然後打印到控制檯。它似乎沒有響應任何點擊,或者至少不會打印它對控制檯的響應。UIButton不會迴應

代碼在下面,如果有需要請告訴我。

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?") 


} 

} 
+1

試試這個:'@IBAction FUNC ratingButtonTapped(按鈕:UIButton的)' –

+1

如果做這樣Mr.UB寫道,不要忘記連接按鈕在故事板中用這行代碼使用CTRL拖動。 – pedrouan

回答

0

請嘗試.TouchUpInside代替TouchDown的

button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchUpInside)