Ben Reed
Ben Reed

Reputation: 874

Trouble with NSTimer

I am having so much trouble with NSTimers. I have used them before, but this timer just does not want to fire.

-(void) enqueueRecordingProcess
{
    NSLog(@"made it!");
    NSTimer *time2 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(warnForRecording) userInfo:nil repeats:YES];

}

-(void) warnForRecording
{
    NSLog(@"timer ticked!");
    if (trv > 0) {
        NSLog(@"Starting Recording in %i seconds.", trv);
    }
}

I don't see why this won't run. I even tried this:

- (void)enqueueRecordingProcess
{
    NSLog(@"made it!");
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(warnForRecording) userInfo:nil repeats:YES];
}

 - (void)warnForRecording
{
    NSLog(@"timer ticked!");
}

What is wrong?

Upvotes: 0

Views: 392

Answers (3)

rmcghee
rmcghee

Reputation: 429

// Yes.  Here is sample code (tested on OS X 10.8.4, command-line).
// Using ARC:
// $ cc -o timer timer.m -fobjc-arc -framework Foundation
// $ ./timer
//

#include <Foundation/Foundation.h>

@interface MyClass : NSObject
@property NSTimer *timer;
-(id)init;
-(void)onTick:(NSTimer *)aTimer;
@end

@implementation MyClass
-(id)init {
    id newInstance = [super init];
    if (newInstance) {
        NSLog(@"Creating timer...");
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
            target:self
            selector:@selector(onTick:)
            userInfo:nil
            repeats:YES];
    }
    return newInstance;
}

-(void)onTick:(NSTimer *)aTimer {
    NSLog(@"Tick");
}
@end

int main() {
    @autoreleasepool {
        MyClass *obj = [[MyClass alloc] init];
        [[NSRunLoop currentRunLoop] run];
    }
    return 0;
}

Upvotes: 0

Grady Player
Grady Player

Reputation: 14549

I am not sure about you specifics, but you need to service the runloop in order to get events, including timers.

Upvotes: 0

kevboh
kevboh

Reputation: 5245

Not sure if this fixes it but from the docs:

The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.

Upvotes: 2

Related Questions