@IBAction func attack(_ sender: UIButton) {
Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.disableAttackBtn(btnTag:sender.tag)), userInfo: nil, repeats: false)
}
func disableAttackBtn(btnTag: Int) {
if btnTag == attack1Btn.tag {
attack2Btn.isEnabled = false
} else {
attack1Btn.isEnabled = false
}
}
尝试将sender.tag数据传递给Timer.scheduledTimer中的函数disableAttackBtn(btnTag :)。但是得到一个错误说“#selector的参数没有引用@objc方法,属性或初始化器”有没有办法将这些数据传递给函数?任何帮助,将不胜感激。感谢
答案 0 :(得分:0)
通过计时器操作传递自定义参数是不可能的。
支持不是参数
disableAttackBtn()
或一个参数,必须是Timer实例
disableAttackBtn(_ timer: Timer)
如果您需要传递自定义参数,请使用userInfo
字典:
Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(disableAttackBtn(_:)), userInfo: ["tag" : sender.tag], repeats: false)
fun disableAttackBtn(_ timer: Timer) {
let info = timer.userInfo as! [String:Int]
let tag = info["tag"]!
...