A simple timer.

Source: Internet
Author: User

Define a global time variable

Nstimer *_timer;

Create a time variable and invoke the Timer method

-(void) viewdidload

{

[Super Viewdidload];

_timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeclick) Userinfo:nil Repeats:YES ];

[_timer firedate];//Start timer

}

#pragma mark-Countdown to exam time

-(void) Timeclick

{

timeout-= 1;

Nsinteger hour = timeout/3600;

Nsinteger minutes = (timeout/60)% 60;

Nsinteger seconds = timeout% 60;

NSString *strtime = [NSString stringwithformat:@ "%.2ld:%.2ld:%.2ld", (long) hour, (long) minutes, (long) seconds];

[Rightbutton settitle:strtime Forstate:uicontrolstatenormal];

if (Timeout <= 0)

{

[_timer setfiredate:[nsdate distantfuture];

[Self showalertview:@ "test time has arrived"];

}

}

-(void) method{

if (_isclick)

{//start

if ([_timer isValid])

{

[_timer invalidate];

_timer = nil;

Label.text = @ "Start";

_isclick = NO;

}

}

Else

{//Pause

Invalidate and re-create the time, the counter is counted from the attribute value, because the attribute holds the hedging

_timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeclick) Userinfo:nil Repeats:YES ];

[_timer Firedate];

Label.text = @ "Pause";

_isclick = YES;

}

}

A simple timer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.