gpt4 book ai didi

ios - 如何通过协调器模式将值从第二个 View Controller 传递到第一个 View Controller

转载 作者:行者123 更新时间:2023-11-28 07:24:11 26 4
gpt4 key购买 nike

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

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

最佳答案

使用委托(delegate)模式或闭包回调模式,您可以将值从第二个 View Controller 传递到第一个 View Controller 。

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

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

关于ios - 如何通过协调器模式将值从第二个 View Controller 传递到第一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com