本文介绍了如何在Objective-C中取消安排NSTimer的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用嵌套的NSTimer.我这里有两个问题.

I am using nested NSTimer in an application. I have two issues here.

  1. 如何在此函数中重新启动时间计数器-(void)updateLeftTime:(NSTimer *)theTimer
  2. 如何终止以前的计时器,因为-(void)updateLevel:(NSTimer *)theTimer 也由计时器调用.
  1. How to re-initiate time counter in this function - (void)updateLeftTime:(NSTimer *)theTimer
  2. How to kill previous timer because - (void)updateLevel:(NSTimer *)theTimer is also calling by timer.


- (void)viewDidLoad {
    [super viewDidLoad];

    tmLevel=[NSTimer scheduledTimerWithTimeInterval:20.0f target:self selector:@selector(updateLevel:) userInfo:nil repeats:YES];

    tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
}

- (void)updateLevel:(NSTimer *)theTimer {
    static int count = 1;
    count += 1;

    lblLevel.text = [NSString stringWithFormat:@"%d", count];

    tfLeftTime.text=[NSString stringWithFormat:@"%d",ANSWER_TIME];

    tmLeftTime=[[NSTimer alloc] init];
    tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
    [self playMusic];

}
- (void)updateLeftTime:(NSTimer *)theTimer {
    static int timeCounter=1;
    timeCounter+=1;
    tfLeftTime.text=[NSString stringWithFormat:@"%d", (ANSWER_TIME-timeCounter)];
}

推荐答案

  • 使用 [tmLevel invalidate] 取消计时器的时间表.
  • 不要忘记在之后立即设置 tmLevel = nil (以避免在Runloop取消计划并释放计时器后使用变量)
  • 在丢失对tmLevel的引用之前,不要忘记使它失效,即在为tmLevel变量分配新的NSTimer之前也调用 [tmLevel invalidate] (否则先前的计时器将继续运行)在新版本的基础上运行)
    • Use [tmLevel invalidate] to cancel schedule of a timer.
    • Don't forget to set tmLevel=nil immediately after (to avoid using the variable after the timer has been unscheduled and released by the Runloop)
    • Don't forget to invalidate the tmLevel timer before loosing the reference to it, namely call [tmLevel invalidate] also before assigning a new NSTimer to the tmLevel variable (or else the previous timer will continue to run in addition to the new one)
    • 还请注意,在您的代码中,您有无用的分配,而且还会造成泄漏:

      Note also that in your code you have useless allocations that are moreover creating a leak:

      tmLeftTime=[[NSTimer alloc] init];
      tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
      

      在这里分配一个NSTimer实例,将该实例存储在 tmLeftTime ...中,然后立即忽略此已创建实例,将其替换为使用 [NSTimercheduledTimerWithTimeInterval:]创建的另一个实例...] !因此,使用 [[[NSTimer alloc] init] 创建的NSTimer丢失了,并且正在造成泄漏(因为它将永远不会被释放).

      here you allocate an NSTimer instance, store this instance in tmLeftTime... and then immediately forget about this created instance to replace it with another one, created using [NSTimer scheduledTimerWithTimeInterval:...]!Therefore, the NSTimer created using [[NSTimer alloc] init] is lost, and is creating a leak (as it will never be released).

      您的第一行完全没用,就像您在做

      Your first line is totally useless, it's kinda like you were doing

      int x = 5;
      x = 12; // of course the value "5" is lost, replaced by the new value
      

      这篇关于如何在Objective-C中取消安排NSTimer的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 02:13