如何通过协调器模式将值从第二视图控制器传递到第一视图控制器

时间:2019-07-16 15:57:10

标签: ios swift coordinator-pattern

我想在我的iOS应用中使用Soroush Khanlou协调器模式(http://khanlou.com)。我的问题是如何通过协调器将数据传递回我的第一个视图控制器? 在使用协调器模式之前,我使用了一个委托(协议)将数据从第二个视图控制器传递回第一个视图控制器,因为我的第一个视图控制器负责创建和展示第二个视图控制器,这不是一个好的解决方案。所以我决定使用协调器 从我的视图控制器中删除应用导航的工作。 这是场景:

class FirstViewController: UIViewController {
    weak var coordinator: MainCoordinator?

    func showSecondViewController(data: Data) {
    coordinator?.presentSecondViewController(data: data) {[weak self] in
        guard let self = self else { return }
        //Do something
    }
}


class MainCoordinator: NSObject, Coordinator {

    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController

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

    func start(completion: (() -> Void)?) {
        navigationController.delegate = self
        let firstViewController = FirstViewController.instantiate()
        firstViewController.coordinator = self
        navigationController.pushViewController(firstViewController, animated: false)
    }

    func presentSecondViewController(data: Data, completion: @escaping () -> Void) {
        let child = SecondCoordinator(data: data, navigationController: navigationController)
        childCoordinators.append(child)
        child.parentCoordinator = self
        child.start() {
            completion()
        }
    }

    func refresh(data: SomeDataType) {
        //data from second view controller is here but I don't know how to pass it to my first view controller
        //there is no reference to my first view controller 
        //what is the best way to pass data back?
    }
}



class SecondCoordinator: Coordinator {

    weak var parentCoordinator: MainCoordinator?
    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    var data: Data!

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

    convenience init(data: Data, navigationController: UINavigationController) {
        self.init(navigationController: navigationController)
        self.data = data
    }

    func start(completion: (() -> Void)?) {
        let vc = SecondViewController(data: data)
        vc.coordinator = self
        vc.delegate = self. //here I used delegate to get data from second view controller to pass it to first view controller
        navigationController.present(vc, animated: true) {
            completion!()
        }
    }
}

extension SecondCoordinator: SecondViewControllerDelegate {
    func refresh(data: SomeDataType) {
        parentCoordinator?.refresh(data: data) // I need to pass this data to my first view controller
    }
}

1 个答案:

答案 0 :(得分:0)

使用委托模式或闭包回调模式,您可以将值从第二个视图控制器传递给第一个视图控制器。

如果使用委托模式,则数据流如下。 SecondViewController-> SecondViewControllerDelegate-> SecondCoordinator-> SecondCoordinatorDelegate-> FirstCoorfinator-> FirstViewController

如果使用闭包回调模式,则将传递闭包作为的参数 SecondCoordinator的启动方法或初始化程序。