Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Hot to start the Timer in NSView by pressing a button. Help

Hot to start the Timer in NSView by pressing a button. Help
Thread Tools
Fresh-Faced Recruit
Join Date: Apr 2008
Status: Offline
Reply With Quote
Apr 12, 2008, 12:01 PM
 
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)initWithFrameNSRect)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)drawRectNSRect)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
     
Junior Member
Join Date: May 2000
Location: USA
Status: Offline
Reply With Quote
Apr 12, 2008, 11:44 PM
 
Using your exact same code, it worked just fine for me. Debugger console output:

2008-04-12 21:41:29.444 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:32.855 Sandbox[7596:10b] GameView.buttonPressed
2008-04-12 21:41:33.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:33.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:34.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:34.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:35.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:35.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:36.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:36.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:37.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:37.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:38.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:38.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:39.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:39.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:40.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:40.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:41.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:41.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:42.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:42.858 Sandbox[7596:10b] GameView.drawRect
2008-04-12 21:41:43.856 Sandbox[7596:10b] GameView.stopTimer
2008-04-12 21:41:43.858 Sandbox[7596:10b] GameView.drawRect
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness.
     
Fresh-Faced Recruit
Join Date: Apr 2008
Status: Offline
Reply With Quote
Apr 13, 2008, 08:41 PM
 
Thank you for your reply. I must be doing something wrong in IB then. How did you set this up in IB?
     
Fresh-Faced Recruit
Join Date: Apr 2008
Status: Offline
Reply With Quote
Apr 14, 2008, 05:10 PM
 
I've found my error. I connected button to GameView directly and it worked. Thanks.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:19 PM.
All contents of these forums © 1995-2008 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.7.2 © 2000-2008, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.2.0 RC8