Advertisemen
I am assuming that all of you guys have basic knowledge of xCode and objective-c because I am not going to explain codes line by line. The purpose of this tutorial is to provide codes and basic knowledge on the topic.
Ok, let's take a look how to make timer in xCode with datePicker.
First of all, select a storyboard then drag a datePicker from Object Library to View Controller. With the picker selected in View Controller change its Mode to Count Down Timer in Attribute Inspector.
Also, grab One Label and Button (name it "Start") while you are there.

lass="separator" style="clear: both; text-align: center;">
After that, switch to viewController.m file and declare three variables -
Now, Inside IBAction function, we just declared in viewController.h, type followings-

In here, countDownInterval grabs the values from datePicker(Count Down Timer), remainder holds the values just grabbed from picker. Where, remainder%60 means leftover values after remainder value is divided 60 (we divide by 60 coz hours, mins and seconds are the combination of 60).
afterRemainder holds the value just calculated so we can use later in updateCountDown function.
Last part, NSTimer calls function updateCountDown(we are about to call) every one second.
And, define the function we just mentioned in button action like this way.

I think, there are not much to explain coz it is a formula
So, overall, your code should look like this.

Now, go ahead and hit build and run in xCode, you should see this.


Now, Inside IBAction function, we just declared in viewController.h, type followings-

In here, countDownInterval grabs the values from datePicker(Count Down Timer), remainder holds the values just grabbed from picker. Where, remainder%60 means leftover values after remainder value is divided 60 (we divide by 60 coz hours, mins and seconds are the combination of 60).
afterRemainder holds the value just calculated so we can use later in updateCountDown function.
Last part, NSTimer calls function updateCountDown(we are about to call) every one second.
And, define the function we just mentioned in button action like this way.

I think, there are not much to explain coz it is a formula
So, overall, your code should look like this.

Now, go ahead and hit build and run in xCode, you should see this.

If you didn't understand any of them, you can ask me in the comment box below, I will try to answer as soon as possible.
Advertisemen
Tidak ada komentar:
Posting Komentar