1

I have two coordinators for Flow A and Flow B.

They look like this:

final class HomeCoordinator: Coordinator {

var navigationController: UINavigationController

init(navigationController: UINavigationController = UINavigationController()) {
    self.navigationController = navigationController

So for each coordinator I start the flow with an UINavigationController.

Let's say that coordinator with Flow A needs to display CommonViewController, but the coordinator with Flow B would also like to show the CommonViewController.

Since the coordinator is injected in CommonViewController, it can't be both CoordinatorA or CoordinatorB. So to perform coordinator operations I've added a delegate that looks like this:

protocol CommonViewControllerDelegate: AnyObject {
   func showAnotherViewController()
}

class CommonViewController: UIViewController {
   weak var delegate: CommonViewControllerDelegate?

But with this approach I have duplication of code because both CoordinatorA and CorodinatorB should implement showAnotherViewController method. And I have multiple view controllers like this, sometimes the delegate doesn't work properly and it's a chaos.

How can I solve this problem? I thought about having one coordinator, but I prefer to keep them separate so I can instantiate one UINavigationController per Coordinator.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
manubrio
  • 419
  • 6
  • 16
  • Could you elaborate a little more, what chaos are you seeing? – Timmy Jul 29 '22 at 08:07
  • @NoeOnJupiter, lets say I have more than 1 view controller to reuse, I have to implement the delegate in both coordinatorA and coordinatorB even if the flows are the same. So CoordinatorA and CoordinatorB has different methods that are the same. Plus, if Coordinator A should show CommonViewController, and CommonViewController should show DetailViewController, and both Common and DetailViewController should show a ProfileViewController, there are 2 delegate that point in the same coordinator and perform the same actions... – manubrio Jul 29 '22 at 08:15
  • 1
    I'm surprised this has so few views and no real answer. The one answer suggesting inheritance seems nice on paper, but falls apart or results in some crazy inheritance structure if you have 5+ views that can be displayed anywhere in the app. – Raimundas Sakalauskas Jan 03 '23 at 20:55

1 Answers1

1

I am not Swift guy, but give me a try. If you want to share the same behaviour between classes, then you can use composition or inheritance.

Let me show an example of inheritance via C#:

public class AnotherViewController
{
    public string Show() 
    {
        return "Show AnotherViewController";
    }
}

public class CoordinatorA : AnotherViewController
{
}


public class CorodinatorB : AnotherViewController
{
}

and an implementation with composition would look like this:

public class AnotherViewController
{
    public string Show() 
    {
        return "Show AnotherViewController";
    }
}

public class CoordinatorA 
{
    private AnotherViewController _anotherViewController;

    public CoordinatorA()
    {
        _anotherViewController = new AnotherViewController();
    }
}


public class CorodinatorB
{
    private AnotherViewController _anotherViewController;

    public CorodinatorB()
    {
        _anotherViewController = new AnotherViewController();
    }
}

It is worth to read also this topic when to choose composiiton or inheritance.

StepUp
  • 36,391
  • 15
  • 88
  • 148