Reputation: 85
This the first time am working with dependency injection in swift an this is how I do it , I have a dataManager that is initialized in appDelegate then passed to my first view controller witch will then be passed to other view controllers every time they get pushed to screen :
if let navigationCtrl = window?.rootViewController as? UINavigationController {
if let firstViewController = navigationCtrl.topViewController as? FirstViewController {
firstViewController.dataManager = dataManager
}
}
Now doing this, everything works great, but when I go to test each view controller, I always get dataManger to be nil, and I know it's because of the condition I gave in the first place. So is there way to make sure the topViewController always has dataManager variable without inheriting from another ViewController.Thank you
Upvotes: 0
Views: 292
Reputation: 423
One of these is happening:
window
is nil
window?.rootViewController
is not a UINavigationController
navigationCtrl.topViewController
is not a FirstViewController
firstViewController.dataManager
is nil
It should be a simple debug. Either put some breakpoints and/or print
statements to know where the problem is.
Tip(s):
Upvotes: 1