从ViewController 1到ViewController 2的Segu,可以让VC2知道VC1已完成加载异步数据



有这篇文章回复:在视图控制器之间传递数据:

在视图控制器之间传递数据

我不认为我在那里所处的东西。

在ViewController 1中,我从Firebase加载异步数据,我想访问ViewController 2,而无需等待VC 1返回所有数据。

在VC2上,有一个我想要禁用直到VC 1完成的按钮。

我确实尝试在数据完成加载后,然后在VC2循环中将属性设置为true,直到该属性返回true,但一旦我从VC1中删除,它就不会在我的Singleton中设置属性在背景中为真。

thx。

当然有很多解决方案,您可能会尝试使用NotificationCenter,做类似:

的事情
// in VC2: let's start listening for VC1 callback
NotificationCenter.default.addObserver(self, selector: #selector(self.callbackFromVC1(_:)), name: Notification.Name("VC1HasFinished"), object: nil)
// in VC2: this is the callback will be executed
func callbackFromVC1(_ notification: NSNotification? = nil) {
  let userInfoFromVC1:[AnyHashable : Any]? = notification?.userInfo
  // do something with userInfoFromVC1
}
....
// in VC1: when your async stuff is finished, push "userInfoFromVC1" into the notification...
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "VC1HasFinished"), object: nil, userInfo:userInfoFromVC1)

最新更新