ios - 如何调用从 View Controller 到应用程序委托(delegate)的所有方法和函数?

标签 ios swift swift2 location nstimer

您好,我正在我的应用程序中使用 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/

相关文章:

iOS CoreLocation(空)崩溃

ios - 使用 iOS9 更改的 UIWebView Navigator userAgent 属性不起作用

swift - 如何正确创建 SF 符号?

ios - 为什么使用 POST 参数的这个成功的 Swift 异步调用没有返回预期的内容

ios - UITableViewCell 多个标签作为一个整体读取,可以分开吗?

iphone - 我如何为 gps 实现监听器?

ios - 在 .podspec 文件中包含一个私有(private) pod 作为另一个私有(private) pod 的依赖项

ios - 仅针对 UITableViewCell 的一部分调用 didSelectRowAtIndexPath

ios - ViewController 委托(delegate)方法未从 AppDelegate 触发

iphone - 如何读取healthkit中的HKWorkoutActivityType.Running?