我对编码相当陌生,所以如果这是一个愚蠢的问题,我深表歉意,但我正在创建一个应用程序,它将(按一下按钮)在标签上显示数组的第一项,然后如果我再次按下该按钮,它将显示第二项,然后如果您再次按下它,则会显示第三项,依此类推。现在我所能做的就是让我的应用程序从数组中选择一个随机项目,然后显示它。
如果您能提供帮助,非常感谢!
这是我的代码:
let prompts = ["A", "B","C","D","E","F","G","H"]
@IBOutlet var PromptLbl: UILabel!
@IBAction func GivePrompt(_ sender: UIButton)
{
let selected = Int (arc4random()) % 8
PromptLbl.text = (prompts[selected])
}
最佳答案
只需声明一个变量并在单击按钮时不断更新其值+1。
let prompts = ["A", "B","C","D","E","F","G","H"]
var currentIndexCount = 0
@IBOutlet var PromptLbl: UILabel!
@IBAction func GivePrompt(_ sender: UIButton)
{
if currentIndexCount < prompts.count {
PromptLbl.text = prompts[currentIndexCount]
currentIndexCount = currentIndexCount + 1
}
}
关于ios - 使用按钮快速循环遍历数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58986801/