您好,我正在我的应用程序中使用 swift2 开发一个应用程序,我想在用户成功登录时每 5 分钟跟踪一次位置,现在我可以成功跟踪用户的位置,现在我想跟踪应用程序何时处于后台,我必须使用应用程序委托(delegate),但所有函数和方法都是我只在 View Controller 中编写的,所以我如何向应用程序委托(delegate)调用 View Controller 方法和函数。
viewController 中的代码:
class ThirdViewController: UIViewController{
In view did load:
{
////// i created timer scheduled with time internal /////
}
////// Here am fetched current locations and performed some function for timer to execute //////
}
在我的appDelegate中:
func applicationDidEnterBackground(application: UIApplication) {
if UIApplication.sharedApplication().applicationState != .Active{
//here i have to call that timer function execute how to do that????
}
最佳答案
当应用程序进入后台时,您可以触发通知
func applicationDidEnterBackground(application: UIApplication) {
if UIApplication.sharedApplication().applicationState != .Active{
NSNotificationCenter.defaultCenter().postNotificationName("Notificationname", object: nil)
}
}
在你的 Controller View 中加载了 dd 观察者以进行通知
// add observer for notification.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.notificationReceived), name: "Notificationname", object: nil)
接收通知的方法处理程序
func notificationReceived() {
// do what you want to do here.
}
关于ios - 如何调用从 View Controller 到应用程序委托(delegate)的所有方法和函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40545145/