ios - 如何让一个 View Controller 从另一个 View Controller 访问变量?

标签 ios xcode swift interface

我在堆栈上看到的所有内容都是通过按下按钮将数据从输入传递到另一个 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/

相关文章:

ios - 当用户单击输入密码时如何使用系统内置密码屏幕对我的应用程序进行身份验证

ios - 使用简单的 Storyboard应用程序获取 SIGABRT 错误

ios - 解析本地数据存储+网络同步

xcode 构建阶段的 Python 脚本

c++ - 如何在 Xcode 9.3 中显示 C++ 代码文档?

swift - 添加为 subview 的 UILabel 数量受到限制

ios - 如果消息通过 Apple Push Notification Service 如何调试?

ios - 在 Swift 中使用 NSStream 连接到 Redis 服务器

c++ - 是否可以在 Xcode 5.1 中使用 std::make_unique?

ios - 检查是否正在运行应用程序或测试 UI