Новый парень учил себя Свифту.Создаю свое первое личное приложение и врезался в стену после нескольких поисков здесь, на YouTube и в Google.Я впервые публикую вопрос (поскольку я смог найти другие ответы здесь).
У меня проблемы с обновлением таймера на UILabel
.Мне удалось найти код на более старых версиях swift, которые запускают таймер и ведут обратный отсчет.Затем я понял, как разбить секунды на минуты и секунды.
Но когда я запускаю приложение, таймер показывает "30: 0" (другая проблема, которую мне нужно выяснить) и никогда не начинает обратный отсчет.Когда я покидаю страницу в симуляторе и возвращаюсь, только тогда обновляется UILabel
.
Я знаю, что viewdidload загружается только при первом открытии страницы.Мне трудно понять, как заставить UILabel обновляться каждый раз, когда меняется секунда.Я не уверен, какой код реализовать.
Большое вам спасибо!
import UIKit
var timer = Timer()
var timerDuration: Int = 1800
// This converts my timeDuration from seconds to minutes and seconds.
func secondsToHoursMinutesSeconds (seconds : Int) -> (h: Int, m : Int, s : Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
class lyricWriteViewController: UIViewController {
//This takes the function to convert minutes and seconds and accepts an input, which I've chosen the variable timeDuration (which is currently 1800 seconds.
var theTimer = (h: 0, m: 0, s: 0)
@IBOutlet weak var countdownTimer: UILabel!
@IBOutlet weak var randomLyric: UILabel!
@IBOutlet weak var titleInput: UITextField!
@IBOutlet weak var lyricInput: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
//This line takes a random array number and shows it on the textlabel.
randomLyric.text = oneLiner
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
//This is the code the does the counting down
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(lyricWriteViewController.counter), userInfo: nil, repeats: true)
}
@objc func counter() {
timerDuration -= 1
// Below is the timer view I've created. It has converted seconds to minutes and seconds but the screen won't refresh. Also, when the seconds number hits zero, it does "0" instead of "00".
let displayTimer = "\(theTimer.m) : \(theTimer.s)"
countdownTimer.text = String(displayTimer)
//When the timer hits 0, it stops working so that it doesn't go into negative numbers
if timerDuration == 0 {
timer.invalidate()
}
}
func submitlyricsButton(_ sender: UIButton) {
//I will eventually tie this to a completed lyric tableview.
}
}