The DetailViewController needs an instance of ImageStore to fetch and store images. You will inject this dependency into the DetailViewController’s designated initializer, just as you did for ItemsViewController and ItemStore in Chapter 10.
In DetailViewController.swift, add a property for an ImageStore.
var item: Item! { didSet { navigationItem.title = item.name } } var imageStore: ImageStore!
Now do the same in ItemsViewController.swift.
var itemStore: ItemStore! var imageStore: ImageStore!
Next, still in ItemsViewController.swift, update prepare(for:sender:) to set the imageStore property on DetailViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // If the triggered segue is the "showItem" segue" switch segue.identifier { case "showItem"?: // Figure out which row was just tapped if let row = tableView.indexPathForSelectedRow?.row { // Get the item associated with this row and pass it along let item = itemStore.allItems[row] let detailViewController = segue.destination as! DetailViewController detailViewController.item = item detailViewController.imageStore = imageStore } default: preconditionFailure("Unexpected segue identifier.") } }
Finally, update AppDelegate.swift to create and inject the ImageStore.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // Create an ItemStore let itemStore = ItemStore() // Create an ImageStore let imageStore = ImageStore() // Access the ItemsViewController and set its item store and image store let navController = window!.rootViewController as! UINavigationController let itemsController = navController.topViewController as! ItemsViewController itemsController.itemStore = itemStore itemsController.imageStore = imageStore