views:

89

answers:

1

I'm writing an application that has a timer functionality built in. Unfortunately, I'm having problems with the NSTimer and I'm not sure what I'm doing wrong. Here is where I'm declaring the timer...

if(!myTimer)
{
    NSLog(@"Setting up the timer!");
    myTimer=[NSTimer timerWithTimeInterval:1 
                                    target:self
                                  selector:@selector(timerTicked)
                                  userInfo:nil 
                                   repeats:YES];
}

Thanks to the NSLog function, I know the code to set the timer up is going off, but it isn't calling the function:

-(void)timerTicked:(NSTimer*)theTimer
{
//NSLOG that tells me that this function isn't being fired
}

Anyone have any idea what I'm doing wrong?

+3  A: 

Your missing a trailing colon on your selector name. Should be something like this

selector:@selector(timerTicked:)

-- added after questioner comment

If it still doesn't work, check to make sure you are adding the timer to a run loop

[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];

http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/doc/uid/20000319-CHDECCEE

See the Discussion segment of the docs, it talks about how to add the timer to the run loop and points to the run loop docs, too.

Joshua Smith
Still doesn't work.
RonLugge
OK, now that solves the problem. I actually changed to 'scheduledTimerWithTimeInterval'; I had the wrong function.
RonLugge