Reputation: 441
I tried to call [self setNeedsDisplay:YES] in a NSTimer selector in order to trigger the drawRect method.
At first, I put the NSTimer init code in a button func:
-(IBAction)buttonPush:(id)sender
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(myTimerAction:)
userInfo:nil
repeats:YES];
}
-(void)myTimerAction:(NSTimer *) timer
{
[self setNeedsDisplay:YES];
}
The "setNeedsDisplay" is called normally but the code inside the drawRect is never called:
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"drawRect");
}
Then I tried to move the NSTimer init code to "- (id)initWithFrame:(NSRect)frame", then everything works just fine. (the drawRect is called correctly every 1 sec).
What's the difference between the two methods above? What should I do if I want to trigger the Timer in a button?
Upvotes: 1
Views: 635
Reputation: 17317
Since it works when you use initWithFrame:, the problem is probably that buttonPush: isn't hooked up correctly. Try setting a breakpoint in buttonPush: an see if it is actually called when you click the button.
Upvotes: 0
Reputation: 688
Just wondering, in what class does that code reside? I would assume the buttonPush: action is inside a controller, correct?
If so, then you should have:
-(void)myTimerAction:(NSTimer *) timer
{
[[self view] setNeedsDisplay:YES];
}
because setNeedsDisplay: is a method of NSView, not NSViewController.
(BTW probably the reason why it works if you put it inside initWithFrame: is because that one is a NSView initializer: I'm guessing that when you move the code there you are also moving the myTimerAction: method, which then has "self" referring correctly to the view.)
Upvotes: 1