我有一个RootVC
,其中包含MapVC
和ListVC
的容器视图。
由于我需要进行API调用并在MapVC
和ListVC
之间共享结果,因此我创建了RootVCDelegate
。
protocol RootVCDelegate {
func fetch(results: [Object]) -> Void
}
prepare(for UIStoryboardSegue:)
中的我根据标识符分别将RootVCDelegate
属性设置为MapVC
或ListVC
。我知道这不起作用,因为委托属性只能有一个值。
委托模式在这里不起作用,那么如何在这些视图控制器之间共享异步调用的结果?
答案 0 :(得分:0)
你可以让RootVCDelegate
成为一组代表,然后当你想对代表进行delegats.forEach { ..... }
时,在分配代表时,只需将代表附加到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
}
}
}
这可能是你要求的?