[英]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. 我想在我的iOS应用程序中使用Soroush Khanlou协调器模式( http://khanlou.com )。 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 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. 如果使用闭包回调模式,则将闭包作为startCoordinator的启动方法或初始化程序的参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.