ios - 在 View Controller 之间共享异步调用的结果

标签 ios swift asynchronous uiviewcontroller

我有一个 RootVC,其中包含 MapVCListVC 的容器 View 。 由于我需要进行 API 调用并在 MapVCListVC 之间共享结果,因此我创建了一个 RootVCDelegate

protocol RootVCDelegate {
    func fetch(results: [Object]) -> Void
}

prepare(for UIStoryboardSegue:) 中,我分别将 RootVCDelegate 属性设置为 MapVCListVC,具体取决于标识符。我知道这行不通,因为委托(delegate)属性只能有一个值。

委托(delegate)模式在这里不起作用,那么如何在这些 View Controller 之间共享异步调用的结果?

最佳答案

您可以将 RootVCDelegate 设为一个委托(delegate)数组,然后当您想要对委托(delegate)执行操作时,执行 delegats.forEach { ..... } 并在分配一个委托(delegate)时执行委托(delegate)只需将委托(delegate)附加到delegates

示例:

// Delegate Protocol
protocol RootVCDelegate {
    fetch(results: [Objects]) //returning Void does nothing so no need to write it
}

// RootVC
class RootVC {
    delegates: [RootVCDelegate]

    prepare(for segue: UIStoryboardSegue) {
        let destination = (segue.destination as? UINavigationController).rootViewController ?? segue.destination  // how I like to handle things if there's a possibility of a UINavigationController housing my UIViewController
        switch destination {
        case _ as MapVC: delegates.append(destination)
        case _ as ListVC: delegates.append(destination)
        default: return
        }
    }
}

这可能就是您所要求的?

关于ios - 在 View Controller 之间共享异步调用的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255593/

相关文章:

iOS autoLayout View 未显示在屏幕上?

iphone - 查找最小/最大纬度和经度

ios - UINavigationController 隐藏了 BarOnSwipe 内存泄漏问题

android - 知道 Firebase 何时完成 API 调用?

c# - 如何在进程停止时取消虚拟主机

ios - 从 View 中移除模糊部分

ios - 使用 AVFoundation 的方形视频

swift - 按下按钮时如何将选择器 View 中的数据存储在变量中?

ios - 如何在 Swift(iOS) 中按月对 UITableView 数据进行分组?

javascript - 执行componentDidMount后无法设置状态