简体   繁体   English

如何调用从视图控制器到应用程序委托的所有方法和函数?

[英]how to call all methods and functions from view controller to app delegate?

Hi am developing a app using swift2 in my app i want to track location for every 5 minutes when user successfully logged in now i can successfully track location of the user now i want to track when the app is in backgrounded for that i have to use app delegate but all functions and methods are i wrote in view controllers only so how can i call view controllers method and function to app delegate. 嗨,我正在我的应用程序中使用swift2开发一个应用程序,我想在用户成功登录后每5分钟跟踪一次位置,现在我可以成功跟踪用户的位置,现在我想跟踪该应用程序何时在后台运行,我必须使用应用程序委托,但所有函数和方法仅在视图控制器中编写,因此如何调用应用程序委托的视图控制器方法和函数。

code in viewController : 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   //////

 }

In my appDelegate : 在我的appDelegate中

func applicationDidEnterBackground(application: UIApplication) {

 if UIApplication.sharedApplication().applicationState != .Active{

 //here i have to call that timer function execute how to do that????

}

You can fire notification when app goes to background 您可以在应用程序进入后台时触发通知

func applicationDidEnterBackground(application: UIApplication) {

 if UIApplication.sharedApplication().applicationState != .Active{

  NSNotificationCenter.defaultCenter().postNotificationName("Notificationname", object: nil)
}
}

in your controller view did load dd observer for notification 在控制器视图中是否加载了dd观察器以进行通知

// add observer for notification.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.notificationReceived), name: "Notificationname", object: nil)

Method handler for received Notification 收到通知的方法处理程序

func notificationReceived() {
// do what you want to do here.
}

You would want to create a class with your function inside it and then call it from your app delegate, you can do this like this: 您可能想要创建一个包含函数的类,然后从您的应用程序委托中调用它,您可以像这样进行操作:

  • Create a swift file with the name of your class eg MyClass.swift 用您的班级名称创建一个swift文件,例如MyClass.swift
  • Then you create the class in your file and add the function in it: 然后,在文件中创建类并在其中添加函数:

class MyClassName { Add your function in here }

  • Then you call it from appDelegate.swift like this: 然后从appDelegate.swift调用它,如下所示:

    let myClassVar: MyClassName?

  • Then call the function like this -> myClassVar.functionName 然后像这样调用函数-> myClassVar.functionName

Like this, 像这样,

func applicationDidEnterBackground(application: UIApplication) {

    if UIApplication.sharedApplication().applicationState != .Active{
      let thirdViewController = ThirdViewController()
      thirdViewController.functionYouWantToCall()
 }

Thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从视图控制器调用应用程序委托方法 - Call app delegate method from view controller 无法从应用程序委托调用View Controller方法 - Can't call View Controller method from app delegate 从View Controller调用TabBarController委托-Swift - Call TabBarController delegate from View Controller - Swift 如果视图控制器是由应用程序委托提供的,如何将其关闭? - How to dismiss view controller if it presented from app delegate? 如何在Swift中的didReceiveRemoteNotification上从App Delegate推送视图控制器? - How to push a view controller from App Delegate on didReceiveRemoteNotification in Swift? 如何从应用程序委托访问视图控制器变量,反之亦然? - How to access view controller variables from the app delegate… and vice versa? 如何从 App Delegate 切换到不同的视图 controller? - How do I switch to a different view controller from the App Delegate? 如何从应用程序委托访问字符串(在视图控制器中) - How to access string (which is in view controller) from the app delegate 如何在应用程序委托中使用视图控制器中的功能? - How to use a function from the view controller in the app delegate? 如何从 swift 中的应用程序委托中关闭当前视图 controller? - How to dismiss current view controller from app delegate in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM