简体   繁体   中英

How to pass a value from second view controller to first view controller through coordinator pattern

I want to use Soroush Khanlou coordinator pattern ( http://khanlou.com ) in my iOS app. My problem is how can I pass data back to my first view controller through coordinators? Before using the coordinator pattern, I used a delegate(protocol) to pass data back from second view controller to the first one, because my first view controller was responsible for creating and presenting the second view controller, that was not a good solution. So I decided to use coordinator to remove the job of app navigation from my view controllers. This is the scenario:

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
    }
}

Using delegate pattern or closure callback pattern, you can pass a value from second view controller to first view controller.

If you use delegate pattern, data flow is below. SecondViewController -> SecondViewControllerDelegate -> SecondCoordinator -> SecondCoordinatorDelegate -> FirstCoorfinator -> FirstViewController

If you use closure callback pattern, you will pass a closure as a parameter of start method or initializer of SecondCoordinator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM