Questions tagged [nstimer]

An Objective C class for creating timer objects.

In Objective C (), you use the NSTimer class to create timer objects or, more simply, timers. A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an NSTimer object that sends a message to a window, telling it to update itself after a certain time interval.

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

NSTimer is “toll-free bridged” with its Core Foundation () counterpart, CFRunLoopTimerRef. See “Toll-Free Bridging” for more information on toll-free bridging.

Additional examples for the NSTimer object can be found here.

2624 questions
5
votes
2 answers

How to create an alarm clock app with swift?

I'm trying to create a kind of alarm clock app with the swift but I could not figure out how to set an alarm model. I've tried UILocalnotification but I don't want my users to be involved the flow of alarm app other than setting the alarm. Then…
Talha ŞEKER
  • 157
  • 1
  • 2
  • 13
5
votes
2 answers

Swift selector - unrecognized selector sent to instance

I have a function: func runRockRotation(rockSprite: SKSpriteNode){ startRockRotationAnimation(rockSprite, isRock: true) } When I call it like this: runRockRotation(rock) it works, but I can't seem to be able to put it inside a…
spacecash21
  • 1,331
  • 1
  • 19
  • 41
5
votes
1 answer

Dynamically update NSMenuItem from an NSTimer while the menu is currently open in swift?

So here's my situation, I have a little app that will keep track of how long you have been working and let you know when it's time to take a break. I have an NSTimer that fires every second and updates some numbers (the remaining time). The app…
Spencer Wood
  • 610
  • 6
  • 15
5
votes
1 answer

iOS , WKInterfaceTimer Start timer

I'm developing an app using WatchKit I want to create one WKInterfaceTimer, when I tap in one button the timer starts with 0 Second but when I run the application the timer automaticaly start and I'm not able to stop timer before or after here is…
katrin
  • 55
  • 3
5
votes
2 answers

iOS Is my UIView visible on screen?

I have a custom UIView that with a timer displays the current time, which is set inside a UITableViewCell. Is there a way to detect that user is no longer viewing this custom UIView I have (say by navigating to a different tab)? I would like to stop…
Bjarte
  • 2,167
  • 5
  • 27
  • 37
5
votes
1 answer

How to Fix Error with a Swift NSTimer Calling Its Selector

I'm getting a runtime error of: 2014-07-15 16:49:44.893 TransporterGUI[1527:303] -[_TtC14TransporterGUI11AppDelegate printCountdown]: unrecognized selector sent to instance 0x10040e8a0 when I use the following Swift code to fire a timer: @IBAction…
P.M.
  • 436
  • 6
  • 12
5
votes
2 answers

Scheduling a Closure Expression in Swift

I'd like to be able to schedule a closure to be run at either an absolute or relative time in the future. I see that I can use NSTimer to schedule a selector to be called later, but this is not what I want. I would prefer to see something like…
cwharris
  • 17,835
  • 4
  • 44
  • 64
5
votes
5 answers

Problem invalidating NSTimer in dealloc

Following is my code: .h file: #import "Foundation/Foundation.h" @interface GObject:NSObject{ NSTimer* m_Timer; } @property(nonatomic, retain) NSTimer* m_Timer; - (void)Initialize; - (void)TimerCallback:(NSTimer*)pTimer; @end .m…
Prashant
  • 937
  • 1
  • 10
  • 23
5
votes
2 answers

How to pause a NSTimer?

I have a game which uses a timer. I want to make it so the user can select a button and it pauses that timer and when they click that button again, it will unpause that timer. I already have code to the timer, just need some help with the pausing…
Kyle Greenlaw
  • 737
  • 1
  • 8
  • 20
5
votes
3 answers

How do I access the dealloc method in a class category?

I need to perform an action in the dealloc method of a category. I've tried swizzling but that doesn't work (nor is it a great idea). In case anyone asks, the answer is no, I can't use a subclass, this is specifically for a category. I want to…
Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
5
votes
2 answers

Cancel NSTimers associated with UITableViewCells when they go offscreen

I implement a UITableView of UIImageView cells, each of which periodically refreshes itself every 5 seconds via NSTimer. Each image is loaded from a server in the background thread, and from that background thread I also update the UI, displaying…
Nimit Pattanasri
  • 1,602
  • 1
  • 26
  • 37
5
votes
2 answers

iOS NSTimer not working?

This is my exact code, and it doesn't seem to be working. Can you tell me what I am doing wrong? Note that refreshTimer was already declared in the private interface. -(void)viewDidLoad { refreshTimer = [NSTimer timerWithTimeInterval:1 target:self…
Redneys
  • 285
  • 1
  • 6
  • 18
5
votes
4 answers

what is the first step in (NSTimer release and invalidate)?

Can I send argument with @selector in NSTimer? If I want to release NSTimer, are the following steps right in dealloc? [timer invalidate]; [timer release];
senthil
5
votes
5 answers

NSTimer with multiple time intervals in a sequence

Without creating multiple NSTimer instances, how would one achieve an NSTimer to fire a specific or multiple method with different intervals in a sequence. For example method1 (0.3 sec), method2 (0.5), method3 (0.7) and so on. I would appreciate if…
Naveed Abbas
  • 1,157
  • 1
  • 14
  • 37
5
votes
4 answers

iPhone register for clock updates

A view in my application has a clock that I'd like to keep synced with the system. A number of Stack questions have centred around NSTimer, but before doing that, I want to check if there is a system notification I can register that is fired every…
MechEngineer
  • 1,399
  • 3
  • 16
  • 27