Chapter 24 Why NC + VC pairs?

In the QuickTodo architechture, each scene has a UINavigationController + UIViewController pair. I played with a similar solution using just VCs without the pairing NCs. I replaced “return nc” with “return vc” in Scene+ViewController.swift. Going further, I got vc directly from storyboard instead of getting its NC first. It seems to me this solution works too.

So my question is, what is the design consideration of the NC + VC pairs? What did I miss when removing NCs by using VCs only? Thanks a lot in advance.

@fpilet Do you have any feedback regarding this? Thank you - much appreciated! :]

There are two cases when you can to use NC + VC pairs:

  1. You are putting up a modal dialog: you want a title bar and possibly a way to perform some navigation inside the modal, use a NC + VC pair instead of simply a VC

  2. You are using a tabbed design: each of your tabs has navigation and requires a navigation controller

You can perfectly get away with using a single navigation controller, although this will cater to only the simplest applications. Knowing that you can use a NC + VC pair is very handy in many cases.

Thanks a lot for shedding light on this!

BTW, it seems there is a small bug in the downloaded code. I had to replace these 2 lines

navigationController.pushViewController(viewController, animated: true)
currentViewController = SceneCoordinator.actualViewController(for: viewController)
(crashed with: *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Pushing a navigation controller is not supported’)


currentViewController = SceneCoordinator.actualViewController(for: viewController)
naviagtionController.pushViewController(currentViewController, animated: true)

to make it work.

Note that the sample itself doesn’t go through this logic path because there is only a modal popup VC - no “push” path covered.

@fpillet How does SceneCoordinator serve a tabs based app? Do we need a SceneCoordinator instance for each tab? How can we centralize navigation among tabs and within each tab? thx!

Oops didn’t see your question earlier, sorry about that. I only use a single SceneCoordinator throughout my apps. There are some adjustments one can make to accomodate tab-based UIs (i.e. additional methods in SceneCoordinator to switch tabs and observe the current tab). Beyond that, it’s pretty straightforward: pushing a new screen is done on the VC for the current tab, same for pop.

Whether you need multiple coordinators is a matter of taste but, since the architecture I highlight has no business logic in the coordinator itself (it just handles the gory details of pushing and popping the screens), one is typically enough.

This topic was automatically closed after 166 days. New replies are no longer allowed.