【发布时间】:2014-11-18 11:46:34
【问题描述】:
我正在为 iPhone 开发一个计时器应用程序。但是当切换视图并返回初始计时器视图时,
label 未更新。虽然我可以看到它仍在打印日志中运行。
我的viewDidLoad 中有以下代码。
当我再次进入timer 视图时,如何再次开始刷新label?
另一个视图是通过 Segue 处理的。
func updateTime() {
var currentTime = NSDate.timeIntervalSinceReferenceDate()
//Find the difference between current time and start time.
var elapsedTime: NSTimeInterval = currentTime - startTime
//calculate the minutes in elapsed time.
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
//calculate the seconds in elapsed time.
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
//find out the fraction of milliseconds to be displayed.
let fraction = UInt8(elapsedTime * 100)
//add the leading zero for minutes, seconds and millseconds and store them as string constants
let strMinutes = minutes > 9 ? String(minutes):"0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds):"0" + String(seconds)
println("----------")
println("currentTime")
println (currentTime)
println("elapsedTime")
println (elapsedTime)
println("extraTime")
println (extraTime)
println("summed")
println (summed)
//concatenate minuets, seconds and milliseconds as assign it to the UILabel
displayTimeLabel.text = "\(strMinutes):\(strSeconds)"
}
@IBAction func start(sender: AnyObject) {
if (!timer.valid) {
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
}
@IBAction func stop(sender: AnyObject) {
timer.invalidate()
}
【问题讨论】:
-
您是否尝试将代码移至 viewWillAppear?
-
点击计时器的开始按钮时,AppDelegate:UiResponder, UIApplicationDelegate 行中出现 Signal SIGABRT 错误。
-
请发布您如何运行计时器的代码。
-
在上面的初始帖子中添加了该内容。谢谢
标签: ios swift timer switch-statement views