NavigationCoordinator

open class NavigationCoordinator<T> : ContainerCoordinator<T>, UINavigationControllerDelegate where T : UINavigationController, T : Coordinated

The NavigationCoordinator is a specialized ContainerCoordinator for UINavigationControlelrs

It is to be used with a UINavigationController as rootViewController and it has specific logic to cope with the intricacies of said UINavigationController. It has specialized presentation methods that mirror the ones from UINavigationController.

  • This is a mirror of the UINavigationController.viewControllers, useful to test later if a UIViewController was popped or not

    Declaration

    Swift

    private(set) open var currentViewControllers: [UIViewController]
  • Starts the Coordinator.

    In this particular case, it also assigns the rootViewController as the UINavigationControllerDelegate. If you override this method do not forget to call super.start() or the subclass won’t work properly.

    Declaration

    Swift

    open override func start(with completion: @escaping () -> Void)

    Parameters

    completion

    An optional Callback executed at the end.

  • Activates the Coordinator. This is called everytime the Coordinator is about to be presented

    If you override this method do not forget to call super.activate() or the subclass won’t work properly.

    Declaration

    Swift

    open override func activate()
  • Convenience method to set the rootViewController from the UINavigationController, or to pop to it in case it is already on the navigation stack.

    Declaration

    Swift

    open override func root(_ coordinator: Coordinating, animated: Bool = false, completion: (() -> Void)? = nil)

    Parameters

    coordinator

    The Coordinator with the rootViewController to be presented.

    animated

    Whether the transition should be animated or not.

    completion

    The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter.

  • Analog to UINavigationController’s show(_ vc: UIViewController, sender: Any?)

    If a Coordinator+UIViewController is already on the navigation stack, the UINavigationController will be pop to it If you override this method, keep in mind that UIViewController cannot be pushed twice to the same navigation stack, or you will get an error

    Declaration

    Swift

    public override func show(_ coordinator: Coordinating, sender: Any?)

    Parameters

    coordinator

    The Coordinator with the rootViewController to be presented.

    sender

    The sender.

  • Analog to UINavigationController’s popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]?

    Declaration

    Swift

    public func popToCoordinator(_ coordinator: Coordinating, animated: Bool)

    Parameters

    coordinator

    The Coordinator with the rootViewController to be presented.

    animated

    Whether the transition should be animated or not.

  • This method tries to determine if a UIViewController was effectively popped (i.e. is back visually on the navigation stack)

    Declaration

    Swift

    public func checkPoppedBack(viewController: UIViewController)

    Parameters

    viewController

    The view controller whose view and navigation item properties are being shown.

  • Callback called with the Coordinator belonging to the UIViewController that was just popped back.

    Use this to react to the pop back event that happens when a user taps the Back button or slides from left to right on a UINavigationController. By accessing the Coordinator, you have the chance to perform some logic in the wake of this event.

    By default, it activates the Coordinator that was popped back

    Declaration

    Swift

    open func poppedBack(to coordinator: Coordinating?)
  • Called just before the navigation controller displays a view controller’s view and navigation item properties.

    By default it checks if the viewControlelr was popped. If you override this method, do not forget to call checkPoppedBack(viewController: UIViewController)

    Declaration

    Swift

    open func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool)

    Parameters

    navigationController

    The navigation controller that is showing the view and properties of a view controller.

    viewController

    The view controller whose view and navigation item properties are being shown.

    animated

    true to animate the transition; otherwise, false.

  • This method will sync the current UINavigationControllers.viewControllers so that we can later use it to determine if a UIViewController was popped or not

    If you override this method, do not forget to sync the viewControllers

    Declaration

    Swift

    open func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool)

    Parameters

    navigationController

    The navigation controller that is showing the view and properties of a view controller.

    viewController

    The view controller whose view and navigation item properties are being shown.

    animated

    true to animate the transition; otherwise, false.