Timer Label not updated after switching views (swi

2020-08-04 09:23发布

问题:

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.

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()
        
    }

回答1:

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)"
}

Then inside your "updateTime" function, where you had the line I took out, replace it with a postNotifiction:

NSNotificationCenter.defaultCenter().postNotificationName("UpdateTimer", object: nil)

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)
}

With the observer in viewDidAppear, the updateText function always gets called, and the text is updated even when you switch views and come back.