我有两个Flow A和Flow B协调员。
他们看起来是这样的:
final class HomeCoordinator: Coordinator {
var navigationController: UINavigationController
init(navigationController: UINavigationController = UINavigationController()) {
self.navigationController = navigationController因此,对于每个协调器,我使用一个UINavigationController启动流。
让我们说,流A的协调器需要显示CommonViewController,但是流B的协调器也想显示CommonViewController。
由于协调器是在CommonViewController中注入的,所以不能同时使用CoordinatorA或CoordinatorB。因此,为了执行协调器操作,我添加了一个委托,如下所示:
protocol CommonViewControllerDelegate: AnyObject {
func showAnotherViewController()
}
class CommonViewController: UIViewController {
weak var delegate: CommonViewControllerDelegate?但是使用这种方法,我需要重复代码,因为CoordinatorA和CorodinatorB都应该实现showAnotherViewController方法。我有这样的多个视图控制器,有时委托不能正常工作,这是一种混乱。
我该如何解决这个问题?我考虑过有一个协调器,但我更愿意将它们分开,这样我就可以为每个协调程序实例化一个UINavigationController。
发布于 2022-07-29 10:08:48
我不是斯威夫特人,但试试看。如果希望在类之间共享相同的行为,则可以使用组合或继承。
让我通过继承通过C#展示一个例子:
public class AnotherViewController
{
public string Show()
{
return "Show AnotherViewController";
}
}
public class CoordinatorA : AnotherViewController
{
}
public class CorodinatorB : AnotherViewController
{
}使用作文的实现如下所示:
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();
}
}何时选择组合或继承这个话题也值得一读。
https://stackoverflow.com/questions/73163402
复制相似问题