我正在使用arc4random创建一个测验来随机化我收到的问题。我想知道如何随机选择哪个答案出现在哪个按钮上。下面是一些代码 func Randomize(){
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个按钮都在其中一个按钮上。
答案 0 :(得分:2)
您使用KVC访问按钮
然后保留一个数组标题,然后每次都获得随机标题。
例如
let keysArray = ["answer1","answer2","answer3","answer4"]
func buttonAtIndex(index:Int)->UIButton?{
let key = keysArray[index] as String
let button = self.valueForKey(key) as? UIButton
return button
}
var labels = ["label1","label2","label3","label4"]
func randomLabelWithButton(){
for(var i = 0;i < 4;i++){
let button = self.buttonAtIndex(i)
let maxIndex = labels.count
let randomIndex = Int(arc4random()) % maxIndex
button?.setTitle(labels[randomIndex], forState: UIControlState.Normal)
labels.removeAtIndex(randomIndex)
}
}
答案 1 :(得分:1)
您可以尝试以下内容:
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))")
答案 2 :(得分:1)
让我以一种非常简单的方式回答: -
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<action-listener>org.primefaces.application.DialogActionListener</action-listener>
<navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler>
<view-handler>org.primefaces.application.DialogViewHandler</view-handler>
</application>
答案 3 :(得分:0)
这里已经给出了答案我正在分享我的方式!
// Your Questions and Answer array should be like this. It would be easily managable.
// "questionsAnswers" should be global
self.questionsAnswers = [["question": "What greeting would you use in the morning?",
"answers": ["Hello", "Goodbye", "Good Morning", "See you soon", "Ni Hao", "CTA"],
"correctAnswer": "1"],
["question": "What greeting would you use in the evening?",
"answers": ["Hello", "Goodbye", "Good Evening", "See you soon", "Ni Hao", "CTA"],
"correctAnswer": "2"]]
// Use this function when you want to show in quesiton
self.showQuestion()
// Definition
func showQuestion() {
let index: NSInteger = (NSInteger)(arc4random_uniform(UInt32(self.questionsAnswers.count)))
println(index)
let questionInfo: NSDictionary = self.questionsAnswers.objectAtIndex(index) as! NSDictionary
// Question Label
let label: UILabel = UILabel(frame: CGRectMake(0.0, 0.0, 0.0, 0.0)) // frame: Give it accordingly
label.text = questionInfo.objectForKey("question") as? String
itsSuperView.addSubview(label)
let answers: NSArray = questionInfo.objectForKey("answers") as! NSArray
// "correctAnswerIndex" should be global
self.correctAnswerIndex = (questionInfo.objectForKey("correctAnswer") as! NSString).integerValue
for var i=0; i<answers.count; i++ {
let btn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
btn.frame = CGRectMake(0.0, 0.0, 0.0, 0.0) // frame: Give it accordingly
btn.setTitle(answers.objectAtIndex(i) as! String, forState: UIControlState.Normal)
btn.addTarget(self, action: "answerAction:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = i
itsSuperView.addSubview(btn)
}
}
func answerAction(sender: UIButton) {
let clickedAnswerIndex: Int = sender.tag
if (clickedAnswerIndex == self.correctAnswerIndex) {
println("Correct!")
}else {
println("Wrong!")
}
}