使 Objective-C iphone 中的 ntimer 无效

发布于 2024-12-06 23:47:36 字数 2070 浏览 0 评论 0原文

我在使 NSTimer 失效时遇到问题。 我使用以下函数初始化 NSTimer

-(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");
}

此外,我在其选择器中调用了一个函数updateLeftTime

- (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];
    }

}

在上面的函数中调用了这个函数[selfincreaseDropShots];。 这是这个函数的代码

-(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];

    }

}

在这个函数中,我正在杀死这个计时器,但我做不到。

   -(void) shutLTTimer{
    if ([tmLeftTime isValid]) {
        [tmLeftTime invalidate];
        tmLeftTime=nil;
    }

} 这是我的应用程序的整个场景

请帮我看看问题是什么。

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 invalidate];
        tmLeftTime=nil;
    }

}
This is the whole scenario of my application

Please help me what is the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2024-12-13 23:47:36

在评论中对话后回答

代码是否确实到达行,[tmLeftTime invalidate];?您可以使用断点或 NSLog 来检查这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文