Swift3 Timer.scheduledTimer问题

时间:2017-03-28 19:56:13

标签: swift3

@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方法,属性或初始化器”有没有办法将这些数据传递给函数?任何帮助,将不胜感激。感谢

1 个答案:

答案 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"]!
    ...