这是我的代码。每次点击addButton时,我都会收到一个无法识别的选择器发送到实例崩溃/错误。我找了一个答案的过去一小时,却找不到答案。请帮忙。
@IBOutlet var addButton: UIButton!
@IBOutlet var resetButton: UIButton!
var number:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let size: CGSize = self.view.frame.size;
countLabel.frame = CGRect(x: 0, y:0, width: size.width, height: size.height/4)
minusButton.frame = CGRect(x:0, y: size.height/4, width: size.width/2, height: size.height/8);
addButton.frame = CGRect(x:0, y:size.height/4 + size.height/8, width: size.width, height: 5*size.height/8)
resetButton.frame = CGRect(x:size.width/2, y: size.height/4, width: size.width/2 ,height: size.height/8)
addButton.addTarget(self, action: #selector(addNumber(sender:)), for: UIControlEvents.touchUpInside)
minusButton.addTarget(self, action: #selector(minusNumber), for: UIControlEvents.touchUpInside)
resetButton.addTarget(self, action: #selector(resetNumber), for: UIControlEvents.touchUpInside)
}
func addNumber(sender: UIButton!) {
number = number + 1
countLabel.text = String(number)
}
答案 0 :(得分:0)
更改
addButton.addTarget(self, action: #selector(addNumber(sender:)), for: UIControlEvents.touchUpInside)
使用
addButton.addTarget(self, action: #selector(addNumber), for: UIControlEvents.touchUpInside)
答案 1 :(得分:0)
您无需在选择器中包含发件人。 因此,您需要将代码更改为
addButton.addTarget(self, action: #selector(addNumber), for: .touchUpInside)
。
并且您不需要隐式解包发件人
func addNumber(sender: UIButton)
答案 2 :(得分:0)
在选择器声明之前添加@objc
关键字:
@objc func addNumber(sender: UIButton!) {
number = number + 1
countLabel.text = String(number)
}