【问题标题】:How to release my NSTimer object in ios?如何在 ios 中释放我的 NSTimer 对象?
【发布时间】:2014-08-25 12:10:43
【问题描述】:

在我的应用程序中,当我从一个视图控制器移动到另一个视图控制器时,我需要释放我的 NSTimer。如何在 ARC 中释放这种类型的对象?我正在使用下面的代码来创建和发布NSTimer,但是我必须在视图控制器中在哪里编写这个发布代码?

为了创造。

- (void)viewDidLoad{
    [super viewDidLoad];
    updateBCK = [NSTimer scheduledTimerWithTimeInterval:(5.0) target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    [updateBCK fire];
}


-(void)changeImage{

            static int i=0;
            if (i == [myImages count]){
                i=0;
            }
            [UIImageView beginAnimations:nil context:NULL];
            [UIImageView setAnimationDuration:0.6];
            mainBackgroundImageView.alpha=1;
            mainBackgroundImageView.image =[myImages objectAtIndex:i];
            NSLog(@"\n The main screen image is %@",[myImages objectAtIndex:i]);
            [UIImageView commitAnimations];
            i++;
        }

用于发布。

[updateBCK invalidate];//
    updateBCK = nil;

提前致谢。

【问题讨论】:

  • 推动新视图控制器的代码在哪里
  • 我正在使用 Timer 在 First View 控制器中随机显示背景图像。
  • 你说当你切换到另一个视图控制器时你想关闭定时器是不是正确的???贴出那部分代码
  • 我正在使用图像数组在我的第一个视图控制器中随机显示,并在第一个视图控制器中的按钮上创建,当我按下按钮时,我需要转到第二个视图控制器,我也有在第一个视图控制器中停止计时器。

标签: ios nstimer


【解决方案1】:

你应该打电话

[timer invalidate];
timer = nil;

你推送你的视图控制器的地方。如果这是一个问题,您仍然可以在

中调用它
- (void) viewWillDisappear:(BOOL)animated;

另外,你应该在

中初始化它
- (void) viewDidAppear:(BOOL)animated;

这更有意义。

【讨论】:

  • 是的,这让我更有意义。终于用下面的代码解决了我的问题。 -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; updateBCK = [NSTimer scheduleTimerWithTimeInterval:(3.0) target:self selector:@selector(changeImage) userInfo:nil repeats:YES]; [updateBCK 火]; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; if ([updateBCK isValid]){ [updateBCK 无效]; updateBCK = 无; } }
  • 干得好。考虑接受这个作为答案:)!
【解决方案2】:

当你想停止时间

使用下面的代码

[timer invalidate];  // timer is the name of timer object

【讨论】:

    【解决方案3】:
    - (IBAction)button:(id)sender {
    
    SecondViewController *second = [[SecondViewController alloc] 
           initWithNibName:@"secondViewController" 
           bundle:nil];
    
    
        [self presentViewController:second animated:NO completion:nil];
    
    [self.timer invalidate];  // timer is the name of timer object
    timer=nil;//it may work without this line too ;not sure
    

    }

    【讨论】:

    • 如果您使用 3 个视图控制器,则此逻辑不起作用。这个逻辑在 2nd 和 3rd View controllers 之间起作用。但不适用于第一个和第二个视图控制器。
    • 这应该在两个视图控制器之间工作与其位置无关。如果它在两个和三个之间工作,它也应该在一个和两个之间工作。
    • 你能用这段代码从一个视图控制器切换到两个视图控制器吗?
    【解决方案4】:

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html

    计时器保持对其目标的强引用。这意味着只要计时器保持有效,它的目标就不会被释放。作为推论,这意味着计时器的目标尝试在其 dealloc 方法中使计时器无效是没有意义的——只要计时器有效,就不会调用 dealloc 方法。

    【讨论】:

      【解决方案5】:

      当我从一个视图控制器移动到另一个视图控制器时

      那么你应该在delloc 中执行此操作,如果你想在你的视图被关闭或释放时做一些清理任务。这是最好的地方,在这种情况下你可以实现它。

      -(void)dealloc{
      
      [updateBCK invalidate];//
          updateBCK = nil;
      
      }
      

      希望对你有帮助

      【讨论】:

      • 在ARC中你不能实现dealloc方法
      • 但是在 ARC 中没有调用 dealloc 方法
      • 我需要在ARC中实现
      • @Mutawe 是的,你可以实现,但你不调用 [super delloc],请不要投反对票,先自己测试。
      • Dealloc 实际上在 ARC 模式下也会被调用。这里的问题是计时器保留了目标(在这种情况下是“自我”)。如果计时器尚未失效,您将面临一个保留周期。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多