views:

400

answers:

2

I'm trying to set up an NSTimer on a delegate - I'm so very new to objective-c so apologies if this doesn't make much sense. But what I have written is:

animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self.delegate selector:@selector(drawView) userInfo:nil repeats:TRUE];

Unfortunately this just doesn't work. Can someone point me in the right direction? My mind has fried!!

+2  A: 

Most likely the method signature for drawView is incorrect. From the NSTimer class reference:

The message to send to target when the timer fires. The selector must have the following signature:

- (void)timerFireMethod:(NSTimer*)theTimer

So, your drawView method should look like this:

- (void)drawView:(NSTimer*)theTimer
{
// Draw the view
}

Also, correct your code to be this (notice the colon following "drawView"):

animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self.delegate selector:@selector(drawView:) userInfo:nil repeats:TRUE];

On a side note, I'm not sure what your drawView is responsible for (I would assume drawing a view). However, there are built-in mechanisms for drawing, which should be followed (except for rare circumstances). Usually, if you have an NSView, you call setNeedsDisplay, which will cause the UI to tell your NSView to redraw itself by calling your NSView's drawRect:. I only mention this since you said you were new to Objective-C, so you might not be aware of this, and end up writing more code than you need to. If you follow this design, you could have your timer call setNeedsDisplay periodically.

CJ
+1  A: 

You've got it right.

Just add a colon in the method name, i.e. @selector(drawView:). Also, by convention objective-c coders use YES and NO.

dk
Ahh thank you very much - worked a treat!!
FlimFlam