invalidate nstimer in objective-c iphone
I am having issue with invalidating a NSTimer
.
I initialize a NSTimer
with below function.
-(void) initLTTimer{
[self shutLTTimer];
[self setQuestion:questionCounter];
isQuestionAttempt=NO;
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
NSLog(@"Timer Initialized");
}
further I called a function updateLeftTime
in its selector.
- (void)updateLeftTime:(NSTimer *)theTimer {
NSLog(@"%d",timeCounter);
timeCounter+=1;
tfLeftTime.text=[NSString stringWithFormat:@"%d", (QUESTION_TIME_LIMIT-timeCounter)];
if (timeCounter>=QUESTION_TIME_LIMIT) {
if (isQuestionAttempt==NO) {
[self increaseDropShots];
}
[self setQuestionBg];
timeCounter=0;
[self shutLTTimer];
[self updateQuestion:nil];
}
}
This function [self increaseDropShots];
called in above function.
here is the code of this function
-(void)increaseDropShots
{
NSString *imgName = @"DactiveRetake";
if (IS_IPAD) {
imgName = [imgName stringByAppendingString:@"_ipad"];
}
wrongAttemp+=1;
for (int i =1; i<=wrongAttemp; i++ )
{
UIImageView *img=(UIImageView *)[self.view viewWithTag:i+50];
[img setImage:[UIImage imageNamed:imgName]];
}
NSLog(@"Question dropped counter: %d",wrongAttemp);
if (wrongAttemp == 3)
{
[self shutLTTimer];
[CommonFunctions initGlobalVars];
[Bgplayer stop];
[Bgplayer release];
OverPageViewController *opvc=[[OverPageViewController alloc] initWithNibName:[CommonFunctions getXIBFile:@"OverPageViewController"] bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:opvc animated:YES];
[opvc release];
}
}
In this function I am killing this timer but I am unable.
-(void) shutLTTimer{
if ([tmLeftTime isValid]) {
[tmLeftTime inva开发者_开发百科lidate];
tmLeftTime=nil;
}
} This is the whole scenario of my application
Please help me what is the issue.
Answered after conversation in comments
Does the code definitely get to the line, [tmLeftTime invalidate];
? You can check this by using a breakpoint or an NSLog
.
精彩评论