如果我导航到两个视图控制器,则不会触发ScheduledTimer

时间:2019-07-31 05:41:17

标签: swift timer

我的scheduleTimer较弱:

weak var timer = Timer.scheduledTimer(timerInterval: 2.0, target: self, selector: #selector(fire), userInfo: ["id",id], repeats: false)

如果我启动此计时器并仅导航到一个viewController,它将在后台正常启动。但是,如果我在计时器触发之前导航到另一个viewController,它将永远不会熄灭。知道为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

  • 定义
class viewController: UIViewController 
{
  var timer = Timer()
  • 用于其他功能
   func one()
    {
        timer =  Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(backClick), userInfo: ["id"], repeats: false)
    }

    func two()
    {
        timer =  Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(backClick), userInfo: ["id"], repeats: false)
    }
  • inValidate()计时器
  override func viewDidDisappear(_ animated: Bool) {
     super.viewDidDisappear(animated)
     timer.fire()
  }

答案 1 :(得分:-2)

因为像@Alexandr Kolesnik的评论那样微弱,或者当第一个消失时您可能无效。