Hi,
I just started learning Cocoa and i need your expertise on timer.
On mainmenu.nib i have a custom view and a button. Inside of custom view i have a timer set up that calls drawRect method every second. If I set up timer in initWithFrame method, timer calls timerTick function which calls drawRect. But when I set up the timer in buttonPressed method, it calls timerTick but doesn't call drawRect method.
How do i resolve this issue? What am i doing wrong? Please help me with this.
Here is my code.
#import <Cocoa/Cocoa.h>
@interface GameView : NSView
{
NSTimer *timer;
}
- (IBAction) buttonPressed : (id) sender;
- (void) timerTick : (NSTimer *) t;
@end
#import "GameView.h"
@implementation GameView
- (id)initWithFrame

NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
/* This works well
timer = [NSTimer scheduledTimerWithTimeInterval : 1.0f
target : self
selector :@selector(timerTick
userInfo : nil
repeats : YES] ;
*/
}
return self;
}
- (void)drawRect

NSRect)rect
{
// Drawing code here.
NSLog(@"GameView.drawRect");
}
- (IBAction) buttonPressed : (id) sender
{
NSLog(@"GameView.buttonPressed");
timer = [[NSTimer scheduledTimerWithTimeInterval : 1.0f
target : self
selector :@selector(timerTick
userInfo : nil
repeats : YES] retain];
}
- (void) timerTick : (NSTimer *) t
{
NSLog(@"GameView.stopTimer");
// this doesn't call drawRect when timer is set up in buttonPressed method
[self setNeedsDisplay : YES];
}
@end