用UiButton反复更改UILabel文本

时间:2017-03-30 05:25:26

标签: ios arrays swift uibutton uilabel

我想用标签展示一些问题。当我按下重置按钮时,阵列中的下一个问题不会出现。我是否应该将问题放在一个数组中,以便出现数组中的下一个元素?

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
}
}

1 个答案:

答案 0 :(得分:0)

这应该适用于您的问题。

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
        }
    }
}

enter image description here enter image description here enter image description here 让我知道它是否适合你。干杯:]