How To Make An Egg Timer For Your iPhone

Matthew Campbell, March 30th

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.

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.

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 a 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 start 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:

Finally, here is the implementation:

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 create 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:

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:

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:

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…

We set the counter variable “i” to 180 because that is how many seconds it takes to boil an egg.

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.

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.

You may simply pass nil for the userInfo parameter here since you generally will not need to use this.

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).

So that is how you can use NSTimer to create your own specialized stop watches or to simply using timing to add animations or other pizzazz to your app.

What Else Could You Use NSTimer For?

How To Make An Egg Timer For Your iPhone…

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….

You must be logged in to post a comment.

  • Hours of videos, demos & screen casts
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.