I have an app that's using Dependency Injection to pass variable data. I followed the tutorial called "The Right Way to Share State Between Swift View Controllers" on the website "EnvatoTuts+" to the dot, but my initial view controller is a Navigation Controller. The AppDelegate is supposed to supply the initial view controller with an injection of my data-class called "ModelController", and then I pass it along through the different view controllers in my app.
How do I pass data from my NavController .class to the first screen that the users ever see, the "ViewController" class? I've never programmed a Navigation Controller and since it's only initially on the screen for that split second when the app launches and there's no buttons to press, I don't know how to make it execute a segue to send the information received from the AppDelegate to the ViewController that pops up when the app loads.
Here's my code:
Code Block | class AppDelegate: UIResponder, UIApplicationDelegate { |
|
| var window: UIWindow? |
|
| func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
|
| print("The app delegate launched.") |
|
| if let homeViewController = window?.rootViewController as? NavController { |
|
| homeViewController.modelController = ModelController() |
|
| print("App delegate homeViewController excecuted successfully.") |
|
| } |
|
| return true |
|
| } |
| } |
Code Block | class NavController: UINavigationController { |
|
| |
|
| var modelController: ModelController! |
|
| |
| override func prepare(for segue: UIStoryboardSegue, sender: Any?) { |
|
| print("This is the NavController segue print test.") |
|
| if let viewController = segue.destination as? ViewController { |
|
| viewController.modelController = modelController |
|
| print("NavViewController segue excecuted successfully.") |
|
| } |
|
| } |
|
| } |
Code Block | class ViewController: UIViewController { |
|
| var modelController: ModelController! |
|
| } |
Code Block | class ModelController { |
|
| |
|
| var thePDFInfo = pdfVariables(theClient: "N/A", theClaimNumber: "N/A", theFileNumber: "N/A", theLossDate: "N/A", theInsured: "N/A", theClaimant: "N/A", thePicsAreBy: "N/A", theDateTaken: "N/A") |
|
| |
|
| } |
Code Block | struct pdfVariables { |
|
| let theClient: String |
|
| let theClaimNumber: String |
|
| let theFileNumber: String |
|
| let theLossDate: String |
|
| let theInsured: String |
|
| let theClaimant: String |
|
| let thePicsAreBy: String |
|
| let theDateTaken: String |
|
| } |