Pausing timer when app is in background state Swift

后端 未结 5 879
攒了一身酷
攒了一身酷 2021-01-02 05:57

My ViewController.swift

func startTimer() {
    timer = NSTimer().scheduleTimerWithTimerInvterval(1.0,target: self,selctor: Selector(\"couting\"),userinfo: n         


        
5条回答
  •  清酒与你
    2021-01-02 06:33

    You are creating a new object and calling pauseTimer() on it:

    viewController().pauseTimer()   // This line causes issue viewController(), creates a new instance
    

    Instead of creating new object, either you should pass that instance to AppDelegate and call pauseTimer() on existing object or Listen for UIApplicationDidEnterBackgroundNotification notification in your view controller class and pause timer from there.

提交回复
热议问题