做一个按钮检查之前是否按下了另一个按钮(Swift)

时间:2017-01-26 18:48:28

标签: ios swift

所以我的情况是: 我有三个按钮,通过“START”按钮通向同一个屏幕。根据第一个屏幕上按下的按钮,我需要启动按钮来进行不同的操作。

应该如何:

Niveau1 - Niveau2 - Niveau3
--- | --------------- | --------------- | ---
--- + ---------- START --------- + ---
--- | --------------- | --------------- | ---
游戏lvl1 ---游戏lvl2 ---游戏lvl3

-

现在如何发生:

Niveau1 - Niveau2 - Niveau3
--- | --------------- | --------------- | ---
--- + ---------- START --------- + ---
--- | --------------- | --------------- | ---
没什么---没什么 - 游戏lvl3

原因是,“START”按钮连接到Niveau3 我想做一些类似的事情:

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

3 个答案:

答案 0 :(得分:0)

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*/
}

答案 1 :(得分:0)

你有正确的想法。只需继续创建一个全局变量并检查您的价值。

所以创建一个全局的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
}
}

答案 2 :(得分:0)

您可以通过在故事板或编程方式的每个按钮上设置不同的标签号来使用.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{

   }
}