我想让while
循环等到用户点击Xcode上的Swift Playground中的UILabel
。我怎么能这样做?
这是我的循环
func gameLoop() {
while(score >= 0) {
let n = arc4random_uniform(3)
if(n == 0) {
opt1.text = rightStatements.randomElement()
opt2.text = wrongStatements.randomElement()
opt3.text = wrongStatements.randomElement()
} else if(n == 1) {
opt1.text = wrongStatements.randomElement()
opt2.text = rightStatements.randomElement()
opt3.text = wrongStatements.randomElement()
} else if(n == 2) {
opt1.text = wrongStatements.randomElement()
opt2.text = wrongStatements.randomElement()
opt3.text = rightStatements.randomElement()
}
}
}
例如,我想等到用户点击opt1
,opt2
或opt3
然后根据用户点击的内容做一些事情。
答案 0 :(得分:1)
使用按钮代替标签,并为按钮指定tag = 1,2和3。为按钮创建一个IBAction函数,并将所有按钮连接到同一个函数。
将变量'n'设为全局。
var n = Int()
func nextAttempt() {
if(score >= 0) {
n = arc4random_uniform(3)
if(n == 0) {
opt1.text = rightStatements.randomElement()
opt2.text = wrongStatements.randomElement()
opt3.text = wrongStatements.randomElement()
} else if(n == 1) {
opt1.text = wrongStatements.randomElement()
opt2.text = rightStatements.randomElement()
opt3.text = wrongStatements.randomElement()
} else if(n == 2) {
opt1.text = wrongStatements.randomElement()
opt2.text = wrongStatements.randomElement()
opt3.text = rightStatements.randomElement()
}
}
else
{
//Score < 0
//Game Over
}
}
@IBAction func onButtonClick(_ sender: Any)
{
switch(sender.tag)
{
case 1:
if (n==0)
{
//Right button tapped
//Update score if you want
}
else
{
self.nextAttempt()
}
case 2:
if (n==1)
{
//Right button tapped
//Update score if you want
}
else
{
self.nextAttempt()
}
case 3:
if (n==2)
{
//Right button tapped
//Update score if you want
}
else
{
self.nextAttempt()
}
}
}
希望这可以帮助你!!