0

So I'm trying to make a function refresh every 1 second so it updates a label, unfortunately it's not trigger for some reason. Code below with comments.

import UIKit


class ViewController: UIViewController {

    @IBOutlet var tt_CountDown: UILabel!


    var tt_currentDate = NSDate()
    var tt_objCalendar = NSCalendar.currentCalendar()


    override func viewDidLoad() {
        super.viewDidLoad()

        self.timeClassManagement()

       //The timer I'm using to call the function to repeat
        var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timeClassManagement"), userInfo: nil, repeats: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func timeClassManagement(){
        //the function that holds the code for the label below
        tt_CountDown.text = "\(String(tt_hour)):\(String(tt_minute)):\(String(tt_second))"

    }



}
jscs
  • 63,694
  • 13
  • 151
  • 195
user3708761
  • 275
  • 1
  • 6
  • 14
  • The timer isn't actually suppose to stop. It's a clock for the most part. How would I go about proving tt_CountDown is not nil? New to Swift... Thank you for your response! – user3708761 Oct 23 '14 at 01:19
  • If it is not supposed to stop then you are going to run into memory problems if this view is ever reloaded (You will have two timers, then three, then four, etc) – borrrden Oct 23 '14 at 01:20
  • However, where do you actually update tt_hour tt_minute, etc? – borrrden Oct 23 '14 at 01:23
  • At the current moment I'm just concerned with getting the label to update, but I'll certainly take that into account when I get to it. Edit: tt_hour and tt_minute are defined in the timeClassManagement – user3708761 Oct 23 '14 at 01:25
  • Your code works for me. Are you getting any errors in the console? – rdelmar Oct 23 '14 at 01:25
  • No issues in console @rdelmar – user3708761 Oct 23 '14 at 01:28
  • Have you tried putting a log into the timeClassManagement method to confirm whether it's called or not? – rdelmar Oct 23 '14 at 01:31
  • @matt, he would get an "unexpectedly found nil while unwrapping an Optional value" error if the outlet were nil. – rdelmar Oct 23 '14 at 01:35
  • @rdelmar Hey, since the code changes the label wouldn't that be a sign that the function has been called? – user3708761 Oct 23 '14 at 01:36
  • 1
    Did you say it changed the label? I thought the problem was that it wasn't changing the label. – rdelmar Oct 23 '14 at 01:39
  • @rdelmar Ah sorry, the problem is NSTimer isn't updating the label, it's suppose to be counting up from the current time of the day. The label changes and shows the current time, but NSTimer is suppose to be updating it every second to show that its counting up. – user3708761 Oct 23 '14 at 01:42
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63531/discussion-between-rdelmar-and-user3708761). – rdelmar Oct 23 '14 at 01:42
  • So the chat with @rdelmar didn't happen, still stuck on this. – user3708761 Oct 23 '14 at 02:11
  • I'm back in the chat room, of now. – rdelmar Oct 23 '14 at 04:11

2 Answers2

3

The problem is not with he timer failing to call its action method; that is working fine. The problem is in the code you used to update the label (from our chat).

func timeClassManagement(){ 
var tt_objDateFormat = tt_objCalendar.components( .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: tt_currentDate) 

var tt_hour = tt_objDateFormat.hour 
var tt_minute = tt_objDateFormat.minute 
var tt_second = tt_objDateFormat.second 

tt_CountDown.text = "\(String(tt_hour)):\(String(tt_minute)):\(String(tt_second))" 

}

The date you're using, tt_currentDate, is the same every time the method is called. You need to pass in the current date so it will change.

var tt_objDateFormat = tt_objCalendar.components( .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate:NSDate.date())
rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

Just pass a string of the function name to the NSTimer init. Selectors are an ObjC construct, and methods that accept selectors can be given strings in Swift.

First timer should be a property. It'll be gone by the time viewDidLoad ends.

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timeClassManagement", userInfo: nil, repeats: true)

More detail is offered in this answer to a similar question: See the answer here: https://stackoverflow.com/a/24007718/3879

Community
  • 1
  • 1
ray
  • 1,966
  • 4
  • 24
  • 39