How do I modify a view from the appdelegate in xcode 10

I have a label that displays the state of the app. as far as I can tell, there is no way to call UIApplicationDelegate protocol methods inside the ViewController and Views can't be linked to AppDelegate. It's like the app is split into two parts that can't communicate with each other.

1 answer

  • answered 2018-10-22 21:05 Harsh

    You can always using notifications...

    Apple Docs

    Tutorial for listening to application moved to background:

    override func viewDidLoad() {
      super.viewDidLoad()
      let notificationCenter = NotificationCenter.default
      notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), 
        name: Notification.Name.UIApplicationWillResignActive, object: nil)
    }
    
    @objc func appMovedToBackground() {
        print("App moved to background!")
    }