【问题标题】:Invalidate NSTimer inside UIView sub class使 UIView 子类中的 NSTimer 无效
【发布时间】:2016-02-08 12:17:35
【问题描述】:

我正在寻找在 UIView 子类中使用 NSTimer 的解决方案。如果我尝试对另一个视图控制器执行 egue,它会继续在后台运行。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: ios objective-c uiview nstimer


    【解决方案1】:

    NSTimer 必须在您的 UIView 子类中声明为一个属性,以便可以在您引用该视图的 UIViewController 中访问它。

    @property (nonatomic, strong) NSTimer *myTimer;
    

    在执行 segue 时。

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
           [self.myCustomView.myTimer invalidate];
    }
    

    【讨论】:

    • prepareforsegue 不适用于 uiview 子类。请再次阅读我的问题。这是关于在 uiview 子视图中失效
    • prepareForSegue 将在您的 UIViewController 中删除,并且您的 ViewController 将具有您的 UIView 子类的引用。它必须是 ViewController 中的 IBOutlet。
    • 不过,还有 1 分钟 :)
    【解决方案2】:

    尝试通过分配使计时器对象为零

      [timerObject invalidate];
      timerObject  = nil;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多