OGeek|极客世界-中国程序员成长平台

标题: ios - 如何从模态视图 Controller 将数据传递回 View 控件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:58
标题: ios - 如何从模态视图 Controller 将数据传递回 View 控件

所以通常我会为此使用委托(delegate)模式,但这是一个棘手的情况。

View Controller A 呈现 -> View Controller B 呈现 -> View Controller C。

当用户完成 View Controller C 中的步骤后,我将在一次调用中同时关闭 B 和 C

   self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

我想将 View Controller C 中的数据传回给 A。这怎么可能,因为 A 没有对 C 的引用,我该如何实现委托(delegate)?

****编辑:这都是以编程方式完成的,所以我不能使用 unwind segues

***** 解决方案 *******

我发现的最佳解决方案是在 VC A 中添加一个观察者,并在关闭 VC 时将对象发布到 VC C:

self.presentingViewController?.presentingViewController?.dismiss(animated: true) {
        NotificationCenter.default.post(name: Notification.Name("UpdateKeywords"), object: self.account)
    }

不要忘记在 deinit() 中删除 VC A 中的观察者



Best Answer-推荐答案


我通常会在 b 中创建对 A 的引用,然后在 C 中创建对该引用的引用,然后,在您传回 A 之前,对引用进行更改,然后调用 self. presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

关于ios - 如何从模态视图 Controller 将数据传递回 View 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40363377/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4