我有以下代码片段,我正在尝试将目标添加到我创建的按钮中。这给了我一个错误Expected type before "->"
。我究竟做错了什么?我是一个快速的新手,有点混淆正在发生的事情。有人在想我这里发生了什么吗?
//MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)->()), for: .touchDown)
addSubview(button) //This is the line that is indicated as erratic
}
修改
This是我正在跟进的内容(Apple的一个页面)。线路不同。这样它表示错误Type 'RatingControl' has no member 'ratingButtonTapped'
,其中成员在类中定义。
编辑2
以下是方法ratingButtonTapped(_:)->Void
func ratingButtonTapped(button: UIButton){
print("Button pressed ")
}
答案 0 :(得分:2)
您不能将() -> ()
封闭用作selector
。删除->()
#selector(RatingControl.ratingButtonTapped(_:))
并确保方法
func ratingButtonTapped(sender : UIButton) { ... }
或在Swift 3中
func ratingButtonTapped(_ sender : UIButton) { ... }
已实施。
目标self
假定该方法是在当前类中实现的。