简体   繁体   English

使属于UIView子类的计时器无效

[英]Invalidate a timer that belongs to a UIView subclass

I have a repeating timer that belongs to a UIView subclass. 我有一个属于UIView子类的重复计时器。

The class has a nib that loads it and I'm using ARC. 该类有一个可加载它的笔尖,而我正在使用ARC。

I'd like to invalidate the timer when the UIView is either... 我想在UIView为...时使计时器无效...

  1. Removed from its superview 从其superview中删除
  2. The ViewController that contains its superView is popped off the stack. 包含其superView的ViewController从堆栈中弹出。

I can't seem to find a method like viewDidDisappear on UIView. 我似乎无法在UIView上找到类似viewDidDisappear的方法。

Is there any other way to intercept this? 还有其他方法可以拦截这种情况吗?

At the moment, after the ViewController is popped the timer keeps firing and creating NSLog outputs. 此刻,在ViewController弹出后,计时器将继续触发并创建NSLog输出。

For the view controller being popped: just use viewDidDisappear or similar. 对于弹出的视图控制器:只需使用viewDidDisappear或类似的viewDidDisappear There's also UINavigationControllerDelegate that may be useful. 还有UINavigationControllerDelegate可能有用。

For the view itself : have you tried using willMoveToSuperview: method in UIView ? 对于视图本身 :您是否尝试过在UIView使用willMoveToSuperview:方法? I haven't verified this, but in theory the view will move to superview nil when it is removed from its superview. 我尚未对此进行验证,但是从理论上讲,当将其从超级视图中删除时,视图将变为超级视图nil

So try the following in your view: 因此,请尝试以下方法:

- (void)willMoveToSuperview:(UIView *)superview {
    if (!superview) {
        // cancel timers
    }
}

There's also a willRemoveSubview: method, but that would get called on the superview rather than the view being removed. 还有一个willRemoveSubview:方法,但是该方法将在willRemoveSubview:视图上调用,而不是被删除。

您是否尝试过在dealloc使其无效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM