None of the tutorials I've seen on how to setup dependency injection, for sharing state, have a Navigation Controller as the root view controller. I've tried the following:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Set custom settings for UINavigationController
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithTransparentBackground()
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navigationBarAppearance.backgroundColor = #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
// dependency injection
let homeViewController = HomeViewController()
homeViewController.bidDetailsController = BidDetailsController()
return true
}
but since I have HomeViewController embedded in a Navigation Controller, the BidDetailsController is never actually injected into HomeViewController. The result is that when I try to use any values inside BidDetailsController, in any of the views throughout the app, I always get an error that it is nil. How can I solve this issue? How can I inject BidDetailsController through the Navigation Controller (which is the initial vc) to my HomeViewController?
class HomeViewController: UIViewController {
var bidDetailsController: BidDetailsController!
override func viewDidLoad() {
super.viewDidLoad()
title = "HOME"
bidDetailsController = BidDetailsController()
}
@IBAction func menuBtnTapped(_ sender: Any) {
print("Show Menu")
}
@IBAction func newBidBtnTapped(_ sender: Any) {
performSegue(withIdentifier: "homeToInfoSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let infoViewController = segue.destination as? InfoViewController {
infoViewController.bidDetailsController = bidDetailsController
}
}
}
class BidDetailsController {
var exampleVariableOne = String()
var exampleVariableTwo = Bool()
}