Why isn't iphone SDK NSTimer calling the function I told it to?
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)
{
NSLo开发者_如何学编程g(@"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?
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.
精彩评论