【问题标题】:How do I make a counter work when you click a button?单击按钮时如何使计数器工作?
【发布时间】:2020-05-13 02:54:48
【问题描述】:

所以我是编码新手,我想让标签在您单击同一个按钮时更改其文本。这样做是虽然你可以使用变量绑定一个 inter get out 和 if 语句,但是隐藏不起作用。代码如下:

@IBAction func buttonOne(_ sender: Any)
{
    var nextText:Int = 1
    nextText += 1

    if nextText == 2
    {
        labelOne.text = "We have a developing situation."
    }

    if nextText == 3
    {
        labelOne.text = "About forty five minutes ago 15 unknown assailant broke into Chicago 1st State Bank"
    }

    if nextText == 4
    {
        labelOne.text = "They are armed and dangerous and have taken numerous hostages"
    }

    if nextText == 5
    {
        labelOne.text = "Your mission is to take you and your partner in to bank and free the hostages"
    }

    if nextText == 5
    {
        labelOne.text = "You shall enter the bank via helicopter and rappel onto the roof, good luck."
    }
}

【问题讨论】:

  • nextText 清除为类级属性

标签: swift uibutton int label


【解决方案1】:

在按钮点击功能范围之外声明你的 nextText

var nextText:Int = 1
@IBAction func buttonOne(_ sender: Any)
{

    nextText += 1

    switch nextText{
     case 2 : labelOne.text = "We have a developing situation."
     case 3 : labelOne.text = "About forty five minutes ago 15 unknown assailant broke into Chicago 1st State Bank"
     case 4 : labelOne.text = "They are armed and dangerous and have taken numerous hostages"
     case 5 : labelOne.text = "Your mission is to take you and your partner in to bank and free the hostages"
     case 6 : labelOne.text = "You shall enter the bank via helicopter and rappel onto the roof, good luck."
     default : labelOne.text = ""
    }
}

【讨论】:

    【解决方案2】:

    或者你可以用这个,

    var nextText = 0
    var labelText = ""
    
    @IBAction func buttonOne(_ sender: Any) {
    
     switch nextText{
      case 0 : labelText = "We have a developing situation."
      case 1 : labelText = "About forty five minutes ago 15 unknown assailant broke into Chicago 1st State Bank"
      case 2 : labelText = "They are armed and dangerous and have taken numerous hostages"
      case 3 : labelText = "Your mission is to take you and your partner in to bank and free the hostages"
      case 4 : labelText = "You shall enter the bank via helicopter and rappel onto the roof, good luck."
      default : labelText = ""
     }
    
    labelOne.text = labelText
    nextText += 1  
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多