我在堆栈上看到的所有内容都是通过按下按钮将数据从输入传递到另一个 View Controller 。假设我有 var banana
,它是一个字典数组,但是一旦我在 ViewA.swift 中的函数完成加载 banana
,我想要另一个 viewController,比如 ViewB.swift以其认为合适的方式操纵该数据。我没有从一个 View Controller 到另一个的 segue。
编辑:实际上是两个 TableViewControllers****
我查看了 NSNotificationCenter,但这似乎不适用于我的变量类型,它是一个字典数组
最佳答案
使用NSNotificationCenter
来访问数据。
尝试下面的代码
//Sent notification
let dictionary = ["key":"value"]
NSNotificationCenter.defaultCenter().postNotificationName("passData", object: nil, userInfo: dictionary)
//Receive notification
NSNotificationCenter.defaultCenter().addObserver(self,
selector:"myMethod:", name: "passData", object: nil)
//Receive notification method
func myMethod(notification: NSNotification){
print("data: \(notification.userInfo!["key"])")
关于ios - 如何让一个 View Controller 从另一个 View Controller 访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150134/