0

I am using Swift 4 and I am wondering how having a reference to a ViewController in AppDelegate works. I have two questions which I have put in bold. I understand that AppDelegate is a delegate that has functions that get called when the app becomes foreground or background, or even when the user does the "open with" event on a file extension that my app is registered to handle and selects to open that file with my app- AppDelegate lets the app know that took place. 1) But what I don't understand is how AppDelegate should communicate with my Views to tell them that these events happened. I am wondering what the good design pattern is for doing so. My solution is to have a reference to a viewController so here is an example:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var hotSpotRISViewController = HotSpotRISViewController()

  ...

    func application(_ app: UIApplication, open url: URL,
                    options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        hotSpotRISViewController.setURL(theURL: url)
        return true
    }
}

What I don't understand is how this actually works. I thought that AppDelegate looks at Main.storyboard and starts that and that Main.storyboard has it's own reference to HotSpotRISViewController(). 2) How is it that by making a reference to HotSpotRISViewController() in the AppDelegate, that it uses this reference instead?

Adam
  • 2,070
  • 1
  • 14
  • 18
  • 1
    How else is `HotSpotRISViewController` used in your app? Is it the initial view controller, or the destination of a segue, or loaded using its ID...or is this the only way it's used? – Phillip Mills May 08 '18 at 17:53
  • It is the view that my navigation controller segues to initially, and just right now I realized a problem. My labels are unable to be used at runtime when opening a text file with my App, as a result it's as if a new instance has been in fact created and causing all sorts of problems. So I don't know if creating an instance of my view in AppDelegate is working, it's as if it creates a view but doesn't create a working storyboard. Am I correct on this? Thanks in advance. I guess I also don't understand what my app is doing after opening a file externally. Is it using the old instance or a new? – Adam May 08 '18 at 22:43
  • Yes, `HotSpotRISViewController()` creates a new instance and, since you're not creating it from a storyboard, it has no storyboard-defined properties initialized. – Phillip Mills May 09 '18 at 12:10

0 Answers0