
To add this implementation, first make an outlet for the pause button by connecting the button from storyboard to the class. This is how your methods should look at this point. Now run your timer, when it gets to zero seconds left it will stop. Make a new method containing the following code: func runTimer() You can erase the didReceiveMemoryWarning() function we will not need it in this demo.ĥ. For the full project code see the link to Github at the bottom of the page. var timer = Timer() var isTimerRunning = false //This will be used to make sure only one timer is created at a time. Underneath the timerLabel outlet create the following variables: var seconds = 60 //This variable will hold a starting value of seconds. Next leave the Storyboard and go to the View Controller class. I’ll assume you know how to connect the labels and buttons to the proper class.Ĥ. Shortcut: To open the view controller file in the assistant editor from Storyboard hold Option and click on the class file in the Navigation inspector/menu on the left. Connect the label and buttons from the storyboard to the view controller class. I chose Courier New because it is monospaced(all letters/number are the same width)and won’t shift around as the numbers count down.ģ. The font you set for the label, however, will be shown when the program runs. I made it look like a timer, but it would work just as well kept as the word “label.” Note: What you set as placeholder text in the label won’t matter since we’ll provide the labels text within the code. (This label will show the starting time and the counting down in seconds, minutes and hours.) Add three buttons: “Start”, “Pause”, & “Reset”. Make a new view controller in Storyboard, or use the pre-existing default view controller.Create a new “Single View Application,” give it a name, and set the language to Swift.The Timer class was created to make it easy to trigger actions, (like updating labels or firing methods), at specific moments in time. “For example, you could create an Timer object that sends a message to a window, telling it to update itself after a certain time interval.”

“A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object.”

A timer is defined in the documentation, like this:

We will use the Timer class provided by Apple. If you need help with the basics or setting up the UI in storyboard check out for some awesome beginner tutorials. I will assume you have a basic understanding of the Swift programming language, Xcode and the Storyboard. Here you will see how to build a simple timer for iOS using Swift 3.0.
