当app处于后台状态时暂停计时器

时间:2022-07-29 02:46:44

my ViewController.swift

我ViewController.swift

func startTimer() {
timer = NSTimer().scheduleTimerWithTimerInvterval(1.0,target: self,selctor: Selector("couting"),userinfo: nil, repeats: true)
}
func pauseTimer() {
timer.invalidate()
println("pausing timer")
}

and this is appDelegate.swift

这是appDelegate.swift

func applicateWillResignActive(application: UIApplication) {
viewController().pauseTimer()
println("closing app")
}

it is printing pausing timer and closing app but when i open again i see it never paused. how do i do it correctly?

它正在打印暂停计时器和关闭应用程序,但当我再次打开时,我看到它从未停止过。我怎么做得正确?

3 个解决方案

#1


23  

You have to set an observer listening to when the application did enter background. Add the below line in your ViewController's viewDidLoad() method.

当应用程序进入后台时,必须设置一个监听器。在视图控制器的viewDidLoad()方法中添加以下行。

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myObserverMethod:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)

Add the below function to receive the notification.

添加下面的函数来接收通知。

func myObserverMethod(notification : NSNotification) {
    println("Observer method called")

    //You may call your action method here, when the application did enter background.
    //ie., self.pauseTimer() in your case.
}

Happy Coding !!!

编码快乐! ! !

#2


1  

Accepted answer by @Suresh in Swift 3

由@Suresh在Swift 3中接受的答案。

Set an observer listening to when the application did enter background in your ViewController's viewDidLoad() method.

当应用程序在您的ViewController的viewDidLoad()方法中输入背景时,设置一个观察者侦听。

 NotificationCenter.default.addObserver(self, selector: #selector(myObserverMethod), name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

Add the below function to receive the notification.

添加下面的函数来接收通知。

 func myObserverMethod(notification : NSNotification) {

   print("Observer method called")

  //You may call your action method here, when the application did enter background.
  //ie., self.pauseTimer() in your case.

}

#3


0  

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

您正在创建一个新对象,并在其上调用pauseTimer():

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.

与其创建新对象,不如将实例传递给AppDelegate并在现有对象上调用pauseTimer(),或者在视图控制器类中监听UIApplicationDidEnterBackgroundNotification通知并暂停计时器。

#1


23  

You have to set an observer listening to when the application did enter background. Add the below line in your ViewController's viewDidLoad() method.

当应用程序进入后台时,必须设置一个监听器。在视图控制器的viewDidLoad()方法中添加以下行。

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myObserverMethod:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)

Add the below function to receive the notification.

添加下面的函数来接收通知。

func myObserverMethod(notification : NSNotification) {
    println("Observer method called")

    //You may call your action method here, when the application did enter background.
    //ie., self.pauseTimer() in your case.
}

Happy Coding !!!

编码快乐! ! !

#2


1  

Accepted answer by @Suresh in Swift 3

由@Suresh在Swift 3中接受的答案。

Set an observer listening to when the application did enter background in your ViewController's viewDidLoad() method.

当应用程序在您的ViewController的viewDidLoad()方法中输入背景时,设置一个观察者侦听。

 NotificationCenter.default.addObserver(self, selector: #selector(myObserverMethod), name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

Add the below function to receive the notification.

添加下面的函数来接收通知。

 func myObserverMethod(notification : NSNotification) {

   print("Observer method called")

  //You may call your action method here, when the application did enter background.
  //ie., self.pauseTimer() in your case.

}

#3


0  

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

您正在创建一个新对象,并在其上调用pauseTimer():

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.

与其创建新对象,不如将实例传递给AppDelegate并在现有对象上调用pauseTimer(),或者在视图控制器类中监听UIApplicationDidEnterBackgroundNotification通知并暂停计时器。