It seems like I use my iPhone all the time as a simple timer. I time how long it takes to cook a steak on the grill, when the pizza is ready to be picked up and how long I should stay on the treadmill to work off all the steak and pizza I eat.
So for this post, I thought it would be fun to see how to create a timer from scratch specifically for one task like cooking an egg. Of course, I am betting that your app could use this function as well especially if you would like to do something like animate a sequence of images or periodically update a RSS feed.
What We Need Is Called NSTimer
The class that we will need to use to create our egg timer is called NSTimer
. This class works like it sounds, you use it to time things. What ends up happening is that you create a timer and then have it perform some action at an interval that you specify.
Egg Timer App Setup Notes
Before we move on, I just want to make sure you have an idea of the plumbing I set up for this exercise. What I did was create a View-Based application; then I added an UIImageView
, UIButton
and a UILabel
to the View
. I also set some properties to make the app look a bit more unique and connected the appropriate IBOutlets
. The final thing I did before working with NSTimer
was I coded and connected an IBAction
called pressButton
which starts the timer when the user touches the UIButton
.
Here is what the app will look like:
Here is the interface
file for the UIViewController
I started with for this project with the IBOutlets
and IBAction
:
#import <UIKit/UIKit.h>
@interface EggTimerViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIButton *button;
}
@property(nonatomic, retain) IBOutlet UILabel *label;
@property(nonatomic, retain) IBOutlet UIButton *button;
-(IBAction)pressButton;
@end
Finally, here is the implementation:
#import "EggTimerViewController.h"
@implementation EggTimerViewController
@synthesize label, button;
- (void)dealloc {
[label release];
[button release];
[super dealloc];
}
-(IBAction)pressButton{
}
@end
Take Action Each Time
Usually, you will want something to happen at a set interval when you are working with apps like the egg timer. It could be to simply update the GUI each second or you may have something bigger in mind. Try to imagine your timer (which I know we haven’t created quite yet) is out there in your app sending a signal each second (or whatever your time interval will be). You will want something to respond to these signals and do something useful.
We can implement this useful behavior by writing some code and wrapping it up in a method. For my egg timer, I will simply put the following code in a method called timerFires
:
- (void) timerFires{
if(i > 0){
i--;
label.text = [NSString stringWithFormat:@"%i", i];
}
else{
label.text = @"The egg is ready";
[timer release];
timer = nil;
}
}
What this method is doing is displaying a count in the UILabel
, decreasing the value of the counter variable “i
” until it reachers zero. Once the counter reaches zero, the app will display a message that the egg is ready and the timer will be released.
NOTE: Astute readers will note that I am using an integer “i
” and an NSTimer
“timer here. These are declared at the top of the UIViewController
code file right under the @synthesize
statement like so:
#import "EggTimerViewController.h"
@implementation EggTimerViewController
@synthesize label, button;
NSTimer *timer;
int i;
...
Set The Timer In Motion
Now that we have something coded which the timer may use to take action, we need to actually create the NSTimer
object and get it to start sending out its messages. We will put the code to do that in the pressButton
method:
-(IBAction)pressButton{
i = 180;
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerFires)
userInfo:nil
repeats:YES];
[timer fire];
}
If you test this code now, you will see the counter display on your app that will start to decrease when you press the button on the UI. Let’s go and point out some of the details now…
i = 180;
We set the counter variable “i
” to 180
because that is how many seconds it takes to boil an egg.
timer = [NSTimer scheduledTimerWithTimeInterval:1
Next, we start to create our NSTimer
object using the scheduledTimerWithTimeInterval
function. The first parameter we will send along with this function is a number that will indicate the time interval we want our NSTimer
to use. Here, I set “1
″ because I want it to do something each second. You can use decimal places if you want to increase or decrease the timing in your own app.
target:self
selector:@selector(timerFires)
The next two parameters indicate what method will fire for each interval the timer fires. This follows the typical target-action pattern used throughout iPhone OS development.
userInfo:nil
You may simply pass nil
for the userInfo
parameter here since you generally will not need to use this.
repeats:YES
Setting this to NO will only have the NSTimer
fire once. You will usually keep this YES
unless you are using NSTimer
to create a scheduled task (another use of NSTimer
).
That’s It – The Egg Is Ready!
So that is how you can use NSTimer
to create your own specialized stop watches or to simply use timing to add animations or other pizzazz to your app.
What Else Could You Use NSTimer For?
Please share this if you like it!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.