我有一个类似于测验的应用程序。它经常显示所有问题。如何让它们只显示一次,所以在最后一个问题之后会弹出一条消息?
func nextText(){
let randomNumber = Int(arc4random_uniform(4))
var textLabel = "" as NSString
switch (randomNumber){
case 1:
textLabel = "Question1."
break
case 2:
textLabel = "Question2."
break
case 3:
textLabel = "Question3."
break
case 4:
textLabel = "Question4."
break
default:
textLabel = "Question 5"
}
self.textLabel.text = textLabel as? String
}
}
答案 0 :(得分:0)
只是以不同的方式放入案件内部:
func nextText(){
let randomNumber = Int(arc4random_uniform(4))
var textLabel = "" as NSString
switch (randomNumber){
case 1:
textLabel = "Question1."
self.textLabel.text = textLabel as? String
break
case 2:
textLabel = "Question2."
self.textLabel.text = textLabel as? String
break
case 3:
textLabel = "Question3."
self.textLabel.text = textLabel as? String
break
case 4:
textLabel = "Question4."
self.textLabel.text = textLabel as? String
break
default:
textLabel = "Question 5"
self.textLabel.text = textLabel as? String
}
}
答案 1 :(得分:0)
也许您可以将问题存储为数组。然后创建了一个混合问题的新数组。然后运行向用户呈现问题的阵列。一旦你到达阵列中的最后一个问题,你就可以显示你的弹出消息。这是一个例子:
1。从How do I shuffle an array in Swift?
随机播放数组extension Array {
func shuffled() -> [T] {
var list = self
for i in 0..<(list.count - 1) {
let j = Int(arc4random_uniform(UInt32(list.count - i))) + i
swap(&list[i], &list[j])
}
return list
}
}
2。您的问题:
let questions = ["Question1", "Question2", "Question3", "Question4"]
let shuffledQuestions = questions.shuffled()
3。列举您的改组问题:
var questionIndex = 0
for question in shuffledQuestions {
// Present the question to the user
self.textLabel.text = question
questionIndex++
}
4. 一旦用户到达最后一个问题,就会出现弹出窗口。例如。何时questionIndex == shuffledQuestions.count
答案 2 :(得分:0)
定义一个可变数组。将randomNumber添加到数组中。检查randomNumber是否存在于数组中。如果它存在再现一个随机数。
这是你的目标问题 - c:
//Defining an array
@property NSMutableArray *asked;
-(void) nextText{
//This will generate a random number between 1 and 5.
int randomNumber = arc4random() % 5+1;
NSString * textLabel =nil;
//This will check all questions are asked
if ([asked count] ==5) {
self.myLabel.text = @"All questions completed";
return;
}
//This will check is random number asked
if ([asked containsObject:[NSString stringWithFormat:@"%i",randomNumber]]) {
return;
}
//This will add random number to asked questions and will ask the question of random number
else{
[asked addObject:[NSString stringWithFormat:@"%i",randomNumber]];
switch (randomNumber){
case 1:
textLabel = @"Question1.";
break;
case 2:
textLabel = @"Question2.";
break;
case 3:
textLabel = @"Question3.";
break;
case 4:
textLabel = @"Question4.";
break;
case 5:
textLabel = @"Question5.";
break;
}
self.myLabel.text = textLabel;
}
}
//If you want more than limited question, create and fill question array
//After you can ask like this. Change else part with this
else{
[asked addObject:[NSString stringWithFormat:@"%i",randomNumber]];
self.myLabel.text = [questions objectAtIndex:randomNumber];
}
答案 3 :(得分:0)
为什么不在每次显示问题时将问题放在一个数组中并从中弹出?所以你可以这样做:
var questions = ["Question1", "Question2", "Question3", "Question4", "Question5"]
for var i = questions.count; i > 0; {
let randomNumber = Int(arc4random_uniform(UInt32(--i)))
let textLabel = questions[randomNumber] as String
println(textLabel)
questions.removeAtIndex(randomNumber)
}
if questions.count == 0 {
println("should show the popup")
}