问题描述
我正在开发适用于iPhone的计时器应用程序。但是当切换视图并返回初始计时器视图时,
标签
不会更新。虽然我可以看到它仍然在打印日志中运行。
I'm working on a timer app for iPhone. But when switching views and coming back to initial timer view,the label
is not updated. While I can see it's still running in the print log.
我的 viewDidLoad
中有以下代码。
当我再次进入计时器
视图时,如何再次刷新标签
?
另一个视图是通过Segue处理的。
I have the code below in my viewDidLoad
.How can I start refreshing the label
again when I enter the timer
view again?The other view is handled through 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()
}
推荐答案
我在选项卡视图应用程序中遇到了完全相同的问题,并使用NSNotification Center解决了它。为了使它适用于您的情况,您可以创建一个单独的函数来更新文本。
I was having the exact same problem in a tab view application and solved it using the NSNotification Center. To make it work in your case, you could make a separate function just for updating the text.
func updateText(notification: NSNotification) {
displayTimeLabel.text = "\(strMinutes):\(strSeconds)"
}
然后在你的updateTime函数中,你拿到了我用的行,用postNotifiction替换它:
Then inside your "updateTime" function, where you had the line I took out, replace it with a postNotifiction:
NSNotificationCenter.defaultCenter().postNotificationName("UpdateTimer", object: nil)
然后将观察者放在View Controller中ViewDidAppear函数中,文本应该更新:
Then put the observer inside a ViewDidAppear function in the View Controller where the text should be updated:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(false)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateText:", name: "UpdateTimer", object: nil)
}
观察者在viewDidAppear中,updateText函数总是得到调用,即使切换视图并返回,文本也会更新。
With the observer in viewDidAppear, the updateText function always gets called, and the text is updated even when you switch views and come back.
这篇关于切换视图后未更新定时器标签(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!