ios - 如何将数据从模态视图 Controller 传回 View 控件

标签 ios swift

所以通常我会为此使用委托(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 中的观察者

最佳答案

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

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

相关文章:

ios - 禁用特定选项卡栏项目排序的最佳方法

ios - 从 UIViewController 中的 UITableView 删除核心数据

ios - 格式化 JSON 接收到的字符串

ios - 在 UIWebView 中显示本地 HTML 页面

ios - 如何让 NSTimer 在强制退出后继续运行

javascript - 在 NodeJS/JavaScript 中使用 iOS Swift Date()

ios - 如何在 Swift 2.0 中将 [dispatch_block_t] 转换为 NSArray?

ios - 容器应用程序的控制台输出为空

swift - 在 swift 中创建随机数数组的最短代码?

swift - AlamoFire GET api 请求未按预期工作