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.