RootSplitViewController broken logic Chapters 3-5

All the starters as well as final source code examples are broken when it comes to either drag/drop or context menu create a new window on the iPad. I noticed this first in the drag and drop chapter but ignored it than once trying to implement the context menu I notice the same issue. A little more context, the main window shows both master/detail where as the new window only shows the detail as it is a smaller screen. The same issue happens when trying to drag from the master into a new screen.

The issue is created in SceneDelegate configure it seems, where the EntryTableViewController is loaded directly instead of the UINavigationController for the Entry.

This is an issue because the root split view controller than expects the to find a UINavigationController which clearly isn’t the case.

/// SceneDelegate
func configure(window: UIWindow?, with activity: NSUserActivity) -> Bool {
    guard activity.activityType == Entry.OpenDetailActivityType,
      let entryID = activity.userInfo?[Entry.OpenDetailIdKey] as? String,
      let entry = DataService.shared.entry(forID: entryID),
      let entryDetailViewController = EntryTableViewController.loadFromStoryboard(),
      let splitViewController = window?.rootViewController as? UISplitViewController else {
        return false
    }

    entryDetailViewController.entry = entry
    splitViewController.showDetailViewController(entryDetailViewController, sender: self)
    return true
  }
/// RootSplitViewController
override func viewDidLoad() {
    super.viewDidLoad()
    let splitViewController = self
    // ---------
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
    // ---------
    splitViewController.delegate = self
    navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
    splitViewController.primaryBackgroundStyle = .sidebar
  }

Error:

Could not cast value of type 'Journalyst.EntryTableViewController' (0x100ce5db8) to 'UINavigationController' (0x7fff897a31d8).
2020-04-09 07:51:09.761028-0700 Journalyst[3376:50129] Could not cast value of type 'Journalyst.EntryTableViewController' (0x100ce5db8) to 'UINavigationController' (0x7fff897a31d8).

@warplydesigned Thank you for sharing this - much appreciated! I will forward it to the book team.

1 Like