views:

255

answers:

1

Okay, I have searched online and even looked in a couple of books for the answer because I can't understand the apple documentation for the NSTimer. I am trying to implement 2 timers on the same view that each have 3 buttons (START - STOP - RESET).

The first timer counts down from 2 minutes and then beeps.

The second timer counts up from 00:00 indefinitely.

I am assuming that all of the code will be written in the methods behind the 3 different buttons but I am completely lost trying to read the apple documentation. Any help would be greatly appreciated.

+1  A: 

Basically what you want is an event that fires every 1 second, or possibly at 1/10th second intervals, and you'll update your UI when the timer ticks.

The following will create a timer, and add it to your run loop. Save the timer somewhere so you can kill it when needed.


- (NSTimer*)createTimer {

    // create timer on run loop
    return [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES];
}

Now write a handler for the timer tick:

- (void)timerTicked:(NSTimer*)timer {

    // decrement timer 1 … this is your UI, tick down and redraw
    [myStopwatch tickDown];
    [myStopwatch.view setNeedsDisplay]; 

    // increment timer 2 … bump time and redraw in UI
    …
}

If the user hits a button, you can reset the counts, or start or stop the ticking. To end a timer, send an invalidate message:


- (void)actionStop:(id)sender {

    // stop the timer
    [myTimer invalidate];
}

Hope this helps you out.

Jonathan Watmough
First off, thank you so much for the help but I am still a bit confused. What do I use for the UILabel and the IBAction? Your code doesn't seem to have those which should be in there - shouldn't it?
Rob
I *highly* recommend that you pick up a copy of Aaron Hillegass' book, Cocoa(R) Programming for Mac(R) OS X (3rd Edition). If you read this and work through the examples, you will gain a solid understanding of Objective-C and Cocoa, including Interface Builder. Honestly, for $30 and some time, it's probably the best investment you could make.Gotta run, baby to feed!
Jonathan Watmough
Yes, in answer to your comment, I skipped the implementation of your UI. You're correct, you need a UIWindow, with a UIView containing some controls which will generate events when the users tap them. The events will need to be routed to IBAction tagged methods in your view controller. And as you said, a UILabel or two to hold your stopwatch read-outs.
Jonathan Watmough