我有一个标签/文本视图,我想为下一个" String"每隔180秒在我的阵列中
我在这里有一些数组,我需要使用Timer
和标签/文本视图。
我的问题是我需要做什么?
@IBOutlet var ThisLabel: UILabel!
var array = ["a", "b", "c"]
var timer = Timer()
func labelShows() {
timer = Timer.scheduledTimer(timeInterval: 180, target: self, selector: #selector(MyViewController.action), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
//Something here?
}
func action() {
//what in this action?
loveLabel.text = "Something here"
}
答案 0 :(得分:0)
您应该使用变量来存储实际的数组索引,并在每次定时器运行时递增它。要获得溢出,请检查变量是否等于数组的减去1并重置它。
var count = 0
func action() {
loveLabel.text = array[count]
if count == array.count - 1 {
//reset
count == 0
} else {
count += 1
}
}
答案 1 :(得分:0)
请检查:
@IBOutlet weak var label: UILabel!
var array = ["a", "b", "c"]
var i = 0
var timer = Timer()
func labelShows() {
timer = Timer.scheduledTimer(timeInterval: 180, target: self, selector: #selector(MyViewController.action), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
labelShows()
action()
}
func action() {
label.text = array[i]
i = (i + 1) % array.count
}