gpt4 book ai didi

swift - 将数据从工作表传回 ViewController

转载 作者:行者123 更新时间:2023-11-28 12:38:25 25 4
gpt4 key购买 nike

我想将数据从 Sheet 传回 ViewController。
我尝试使用委托(delegate),但它不起作用,因为当我关闭工作表 (self.dismiss(self)) 时,后面的 ViewController 没有刷新。

enter image description here

最佳答案

您将不得不使用称为 NSNotificationCenter 的单例类

在ViewController类中调用presentViewController方法之前添加这条语句

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.funcToBeExecuted(_:)), name:String, object: nil)

在Sheet类的dismissViewController之前添加这条语句

NSNotificationCenter.defaultCenter().postNotificationName(String, object: AnyObject?, userInfo: [NSObject : AnyObject]?))

从技术上讲,这就是它的工作原理。您在应用程序中设置一个观察者,等待应用程序发布何时执行#selector 方法。当应用程序执行 postNotification 语句时,内存中具有与 postNotification 相同的 NotificationName 的所有观察者都会被触发并实现分配给它们的#selectors。

postNotificationName 参数中的 userInfo 有助于将数据从一个地方传递到另一个地方,甚至传递给同名的多个观察者。因此,在 NSNotification 选择器执行的方法中,我们可以访问 userInfo,如下所述。

func funcToBeExecuted(notification: NSNotification)
{
let receivedData = notification.userInfo
}

这应该可以解决问题。

关于swift - 将数据从工作表传回 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com