【发布时间】:2020-11-29 18:41:01
【问题描述】:
我有一个 pickerController 设置小时、分钟和秒,但我不知道如何设置我的计时器,这是我拥有的代码,但它不起作用,我该如何更改?提前致谢
func startTimer() {
if timer.isValid == true || timeHours == 0 || timeMinutes == 0 || timeSeconds == 0 {
//do nothing
} else {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
}
}
@objc func onTimerFires() {
timeHours -= 1
timeMinuteLabel.text = timeFunc()
if timeSeconds == 0 {
timer.invalidate()
}
}
func timeFunc() -> String {
let hours = Int(optionalValuePickerView.selectedRow(inComponent: 0))
let minutes = Int(optionalValuePickerView.selectedRow(inComponent: 1))
let seconds = Int(optionalValuePickerView.selectedRow(inComponent: 2))
return String(format: "%02i:%02i:%02i", hours, minutes, seconds)
}
【问题讨论】:
-
“不起作用”是出了名的无用。你希望它做什么,而不是做什么?
-
@PhillipMills 在 timeFunc() 函数中,我从 PickerView 中获取值并将其以小时、分钟和秒的形式提供给计时器,问题是当我运行它时,计时器什么也不做,小时,分钟和秒仍然没有改变。那是我的问题,我不知道如何使它成为一个有用的功能计时器