title: How to call method in view controller in app delegate?
body: <p>I need to call a method in my ViewController class in AppDelegate. This is what the method looks like this:</p>
  
  <pre><code>class ViewController: UIViewController {
  
      func myFunction() {
          ...
      }
  }
  </code></pre>
  
  <p>I've tried using window.rootViewController, but it's telling me the method myFunction does not exist, since rootViewController is of type UIViewController.  </p>
  
  <p>I've also tried</p>
  
  <pre><code>var vc = ViewController()
  self.vc.myFunction()
  </code></pre>
  
  <p>But this is creating a new instance of the ViewController class, and that's not what I want, either.</p>
  
  <p>Here is the part of the AppDelegate code that I need myFunction to be called from:</p>
  
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {
  
      func handleEvent() {
          if UIApplication.shared.applicationState == .active {
              // call myFunction()
          }
          else {
              ...
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Context:
  I'm following a geofencing tutorial: <a href="https://www.raywenderlich.com/136165/core-location-geofencing-tutorial" rel="noreferrer">https://www.raywenderlich.com/136165/core-location-geofencing-tutorial</a>  </p>
  
  <p>Since user location is being monitored even when app is closed, when an event is triggered, AppDelegate should be used to handle the event, since view controller is not yet loaded.  </p>
  
  <p>In my AppDelegate that handles event, if the application is active, I would like to pop up an alert to have the user choose to stop monitoring location. The function that stops location monitoring (myFunction) is in view controller.</p>