【问题标题】:Make a button check if another button was pressed before (Swift)做一个按钮检查之前是否按下了另一个按钮(Swift)
【发布时间】:2017-01-26 18:48:28
【问题描述】:

所以我的情况是: 我有三个按钮,通过“开始”按钮通向同一个屏幕。根据在第一个屏幕上按下的按钮,我需要那个开始按钮到不同的东西。

应该如何:

尼沃1 -- 尼沃2 -- 尼沃3
--- |---------------|---------------| ---
---+----------- 开始 ---------+---
--- |---------------|---------------| ---
游戏 lvl1---游戏 lvl2 --- 游戏 lvl3

-

现在是如何发生的:

尼沃1 -- 尼沃2 -- 尼沃3
--- |---------------|---------------| ---
---+----------- 开始 ---------+---
--- |---------------|---------------| ---
没什么——没什么——游戏lvl3

原因是“开始”按钮连接到 Niveau3。
我想做类似的东西:

if Start.isPressed {  
  if Niveau1.wasPressed {   
    start Game lvl1     
  }     
  if Niveau2.wasPressed {     
    start Game lvl2     
  }     
  if Niveau3.wasPressed {     
    start Game lvl3     
  }     
}       

【问题讨论】:

    标签: ios swift


    【解决方案1】:
    var a = BooleanLiteralType()
    var b = BooleanLiteralType()
    var c = BooleanLiteralType()
    
    /* in code for button a when pressed*/
     a = true
    if a == true{
    /*run code for game a*/
    }
    
    
    /* in code for button b when pressed*/
     b = true
    if b == true{
    /*run code for game b*/
    }
    
    /* in code for button C when pressed*/
     c = true
    if c == true{
    /*run code for game c*/
    }
    

    【讨论】:

      【解决方案2】:

      你的想法是对的。继续创建一个全局变量并像你一样检查值。

      所以创建一个全局 Int 变量,如:

      var nv1 = 0
      

      然后当用户点击Niveau1,2,3按钮时,改变int值,例如:

      nv1 = 1 //if Niveau1 is tapped
      nv1 = 2 //if Niveau2 is tapped
      nv1 = 3 //if Niveau3 is tapped
      

      然后按照您的代码,它将是:

      if Start.isPressed {
      if nv1 == 1 {
      start Game lvl1
      }
      else if nv1 == 2 {
      start Game lvl2
      }
      else if nv1 == 3 {
      start Game lvl3
      }
      }
      

      【讨论】:

        【解决方案3】:

        您可以通过情节提要或以编程方式在每个按钮上设置不同的标签号来使用 .tag 属性。

        //Programatically set this in either in viewDidLoad
        button1.tag = 1
        button2.tag = 2
        button3.tag = 3
        
        //Assign below action method to all of above button
        @IBAction func startButtonPressed(_ sender: Any) {
        
           let button = sender as! UIButton
           if button.tag == 1{
        
           }
           else if button.tag == 2{
        
           }
           else if button.tag == 3{
        
           }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多