Новенький тут учу себя Swift. Создал свое первое личное приложение и уперся в стену после нескольких поисков здесь, на 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.
}
}
displayTimer
непосредственно перед выполнениемcountdownTimer.text = String(displayTimer)
- person iOSer   schedule 26.10.2018