NSTimer

Discussion in 'Mac Programming' started by liptonlover, Jul 27, 2008.

  1. liptonlover macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    I'm trying to learn how to get a timer working... I read the apple docs on timers and loops. I also went through the loop and nstimer class docs. Finally I went through the chapter on NSTimer in cocoa programming for mac os x. Here's my unsuccessful code. (I just checked, all my connections are good. As far as I can tell it should work. Pressing "go" does nothing though...)


    #import <Cocoa/Cocoa.h>

    @interface AppController : NSObject/* Specify a superclass (eg: NSObject or NSView) */ {
    IBOutlet id progressBar;
    NSTimer *timer;
    int count;
    }
    - (IBAction)go:(id)sender;
    - (IBAction)reset:(id)sender;
    - (IBAction)stop:(id)sender;
    @end


    #import "AppController.h"

    @implementation AppController
    - (IBAction)go:(id)sender {
    timer=[[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:mad:selector(increment:) userInfo:nil repeats:YES] retain];
    }

    - (IBAction)reset:(id)sender {

    }

    - (IBAction)stop:(id)sender {

    }

    - (void)increment:(NSTimer *)aTimer {
    count=count+1;
    [progressBar setIntValue:count];
    }
    @end

    Can anyone tell me what's wrong? Thanks! Nate
     
  2. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    not totally sure about this, but what exactly is "count"?
     
  3. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    NSProgressIndicator is not an NSControl, so there is no setIntValue: method. You need to use incrementBy: or setDoubleValue: after you set up a min-max range in IB or setMinValue: and setMaxValue:

    Also, FYI, a couple more things: You're leaking your NSTimer, since it's never released anywhere, and you probably want to prevent multiple timers from being created in your go: method. (A simple timer == nil check will work.)
     
  4. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    kpua is probably right, but the simple thing to do (for starters) would be to throw a couple NSLog statements in your go: and increment: methods to see if they are getting called at all. I probably use NSLog as my primary debugging method about 90% of the time. Primitive, yes, but it works.
     
  5. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #5
    @darkroom - count is simply an int, hence this line:
    int count;

    @kpua - Thanks I forgot about that... I used them before. I know I'm leaking, I just didn't bother with that bit of code yet. I want to get it working first. Thanks for pointing that out though :)

    @hirez - ok thanks :)
     
  6. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    Also, your app may have stopped responding because of a runtime error. Are you looking at the console while it's running?
     

Share This Page