Swift - 如何在连续点击3次后禁用按钮?

时间:2015-06-17 02:01:38

标签: swift xcode6

我在viewcontroller中有一个按钮。我需要这样做,以便在连续点击3次时禁用该按钮。然后,如果用户点击任何其他按钮(或符合其他条件),则应再次启用该按钮。

1 个答案:

答案 0 :(得分:1)

使用计数器检查连续检查的次数

var counter: Int
@IBOutlet weak var button1: UIButton!

@IBAction func otherButtonTouched(sender: UIButton!) {
    self.counter = 0
    self.button1.enable = true
}

@IBAction func button1Touched(sender: UIButton!) {
    self.counter++;
    if (self.counter == 3) {
        sender.enable = false
    }
}   

函数button1Touched触摸处理程序,用于连续检测3次触摸的按钮。函数otherButtonTouched是重置计数器并启用按钮的任何其他按钮的处理程序。