QuickBirdEng / XCoordinator

🎌 Powerful navigation library for iOS based on the coordinator pattern
MIT License
2.25k stars 177 forks source link

Trigger a NavigationCoordinator inside a Page Coordinator #200

Closed Daniebro5 closed 3 years ago

Daniebro5 commented 3 years ago

I'm trying to trigger a second coordinator as follows:

StepsListRoute: Route {
  case otherCoordinator
}

class StepsPageCoordinator: PageCoordinator<StepsListRoute> {

  override func prepareTransition(for route: StepsListRoute) -> PageTransition {
    switch route {
    case .insuranceOptions:
      let coordinator = otherCoordinator(rootViewController: self.rootViewController)
      return .trigger(OtherRoute.some, on: coordinator)
    }
  }
}

But I'm seeing "Cannot convert value of type 'UIPageViewController' to expected argument type 'NavigationCoordinator.RootViewController' (aka 'UINavigationController')". What am I doing wrong?

chefnobody commented 3 years ago

I could be wrong but if .trigger(OtherRoute.some, on: coordinator) does not return PageTransition type which the overridden function is expecting, you're likely going to get an error of this type.

I believe this is because PageTransition is an alias for Transition<UIPageViewController> and I'm betting that your .trigger(OtherRoute.some, on: coordinator) returns a NavigationTransition which doesn't match the expected return type.

pauljohanneskraft commented 3 years ago

Hey @Daniebro5 !

In the line let coordinator = otherCoordinator(rootViewController: self.rootViewController), you are trying to create a NavigationCoordinator (or a subclass thereof) with a rootViewController of UIPageViewController instead of UINavigationController.

To give a little bit more context:

Each coordinator has a rootViewController it is managing. In the case of a PageCoordinator this is a UIPageViewController, for NavigationCoordinator this is a UINavigationController. When a route is triggered, a transition is created, which is then performed on this exact rootViewController.

Since in your example code, you are trying to create a NavigationCoordinator with a UIPageViewController, the compiler is trying to tell you that not all of the UINavigationController capabilities are available for UIPageViewController (for example pushViewController).

In a normal case, you would probably simply initialize the NavigationCoordinator without specifying a rootViewController (it will simply create one itself then), or specify one - e.g. let coordinator = otherCoordinator(rootViewController: UINavigationController())