我有几个问题想用标签来显示。当我按下重置按钮时,数组中的下一个问题不会出现。我是否应该将问题放入数组中以便数组中的下一个元素出现?
func theFirstTwoQuestions() {
// the questions will be contained in an Array
// change the question label to one of the array values
// when I press reset the next question will appear
//self.questionLabel.text? = "\(questionsOneTwo)"
questionLabel.isHidden = false
var arrayQuestions = ["What's your name?", "How old are you", "your favorite color is?"]
for question in arrayQuestions {
questionLabel.text = arrayQuestions.first
}
resetButton(sender: UIButton)
arrayQuestions.remove(at: 1)
questionLabel.text = arrayQuestions.first // "How old are you" is now the first question
}
}
最佳答案
这应该可以解决您的问题。
func theFirstTwoQuestions()
{
// the questions will be contained in an Array
// change the question label to one of the array values
// when I press reset the next question will appear
questionLabel.isHidden = false
var arrayQuestions = ["What's your name?", "How old are you", "your favorite color is?"]
var count = 0
func onTapResetButton(sender: UIButton)
{
self.questionLabel.text = self.arrayQuestions[count] as? String
if(count < arrayQuestions.count-1)
{
count += 1
}
else
{
count = 0
}
}
}
关于ios - 使用 UiButton 反复更改 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43109212/