Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • NSTimer In Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 221
    Comment on it

    Hi all,

    You can use the NSTimer class to create timer objects or, more simply, timers. A timer can wait and call a method or selector after defined period of time. for example :- if you want show a seconds clock and stop when it completed 60 seconds then you can create a time with 1 second of period and turn on repeat mode , when defining selector method you have to specify some condition whenever you want to stop this timer.

    Declaration :-

    var timer: NSTimer?   
    

    Initialization :-

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

    Start Timer :-

    NSRunLoop.currentRunLoop().addTimer(timer!, forMode:NSRunLoopCommonModes)
    

    Defining Timer Selector :-

    func updateTimer() {
          //DO SOMETHING COOL
            if CONDITION {    // condition to stop timer  
                timer!.invalidate()
                timer = nil
            }
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: