【问题标题】:After a button is tapped, see if it was tapped again?一个按钮被点击后,看看它是否被再次点击?
【发布时间】:2017-02-02 10:12:06
【问题描述】:

所以在 Swift 中,您可以创建自己的 Outlets 和操作,到目前为止一切都很好。然后在动作中,我在里面放了一个按钮,当它被点击时,标签会改变字符串。反正有没有重复这个过程,因为一个水龙头说“你点击了它”,然后如果你再次点击它,它会说“你又点击了它?”等等等等。感谢您的帮助!

@IBAction func Button(_ sender: Any) {
    if(Button.isTouchInside){
        Lable2.text="You Tapped The Button!!!"
    }
}

【问题讨论】:

  • 这是一个很好的答案。此外,如果您正在使用 IB(正如您的 IBAction 所见)并在 .touchUpInside 上触发您的操作,为什么要在代码中添加检查?只需让发送者成为 UIButton!

标签: swift


【解决方案1】:

您需要一个包含计数器的变量。

private var buttonTapCount = 0

@IBAction func buttonTap(_ sender: Any) {
    buttonTapCount += 1
    if buttonTapCount == 1 {
        Lable2.text="You Tapped The Button!!!"
    }
    else if buttonTapCount == 2 {
        Lable2.text="You tapped it Again!!!"
    }
    else {
        Lable2.text="You tapped it \(buttonTapCount) times!!!"
    }
}

【讨论】:

    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2013-05-09
    • 2013-07-26
    相关资源
    最近更新 更多