gpt4 book ai didi

ios - 如何快速随机化 button.setTitle?

转载 作者:行者123 更新时间:2023-11-28 10:24:11 25 4
gpt4 key购买 nike

我正在使用 arc4random 创建一个测验来随机化我收到的问题。我想知道如何随机选择哪个答案出现在哪个按钮上。下面是一些代码 函数随机化(){

    var RandomNumber = arc4random() % 4
RandomNumber += 1

switch (RandomNumber) {

case 1:

Question.text = "What greeting would you use in the morning"
Answer1.setTitle("Hello", forState: UIControlState.Normal)
Answer2.setTitle("Goodbye", forState: UIControlState.Normal)
Answer3.setTitle("Good Morning", forState: UIControlState.Normal)
Answer4.setTitle("See you soon", forState: UIControlState.Normal)
Answer5.setTitle("Ni Hao", forState: UIControlState.Normal)
Answer6.setTitle("CTA", forState: UIControlState.Normal)
CorrectAnswer = "1"
break

编辑:我想要的是所有问候出现在任何按钮上,但所有 6 个都出现在其中一个按钮上。

最佳答案

你可以试试下面的方法:

let Answer1 = UIButton()
let Answer2 = UIButton()
let Answer3 = UIButton()
let Answer4 = UIButton()
let Answer5 = UIButton()
let Answer6 = UIButton()

let buttons = [Answer1, Answer2, Answer3, Answer4, Answer5, Answer6]

var answers = [
"this is answer 1",
"this is answer 2",
"this is answer 3",
"this is answer 4",
"this is answer 5",
"this is answer 6"
]

for button in buttons {
let index = Int(arc4random_uniform(UInt32(answers.count)))
button.setTitle(answers[index], forState: .Normal)
answers.removeAtIndex(index)
}

println("Answer1 title: \(Answer1.titleForState(.Normal))")
println("Answer2 title: \(Answer2.titleForState(.Normal))")
println("Answer3 title: \(Answer3.titleForState(.Normal))")
println("Answer4 title: \(Answer4.titleForState(.Normal))")
println("Answer5 title: \(Answer5.titleForState(.Normal))")
println("Answer6 title: \(Answer6.titleForState(.Normal))")

关于ios - 如何快速随机化 button.setTitle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30024424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com