ios - 使用 UiButton 反复更改 UILabel 文本

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

最佳答案

这应该可以解决您的问题。

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 让我知道它是否对你有用。干杯:]

关于ios - 使用 UiButton 反复更改 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43109212/

相关文章:

ios - 如何在 App Store Connect 中添加本地化?

ios - 如何在单击下一个按钮时播放下一首歌曲

html - 在不滚动页面的情况下隐藏 iPhone 设备中 Safari 的默认元素,如标题栏和状态栏

ios - 如何从 subview Controller 重新定义协议(protocol)扩展

c++ - 如何在C++中复制动态数组的值

javascript - 使用 Array.prototype 仅返回数组数据而不返回函数本身

javascript - 数组部分 JavaScript?

ios - 如何在swift中停止功能

ios - 一起使用 Google Analytics SDK 和 Firebase SDK

ios - 我们可以使用 swift3.0 将一个项目 View Controller 类用于同一工作区中的另一个项目吗