ios - 在 VIewController 之间共享数据 - iOS

标签 ios swift scope singleton

对于创建的任何对象,我通常使用两个两个作用域:1) 单例 2) {本地作用域}。我正在寻找介于两者之间的东西。

假设我有一个对象,有 5 个 View Controller 正在编辑。我想在 View Controller 之间共享一个对象,而不必在 View Controller 之间传递它。但它不应该存在于整个应用程序中,因为一旦我编辑完对象,我就不再需要它了。

我不想从另一个类继承所有 View Controller 并在那里创建一个变量。由于 View Controller 可重用于不同的对象。我想创建一个对象,该对象在启动第一个 View Controller 之前启动,在 5 个 View Controller 的范围内存在,然后在我以某种方式保存它后消失。无论如何,我可以在 iOS 中执行此操作吗?

最佳答案

另一种方法是使用您的 AppDelegate。在其中,您可以声明一个全局变量而不是 2 个函数,第一个用于获取当前值,另一个用于设置值。

它可能会给出这样的结果:

// Get AppDelegate instance
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate;

// Use your getter to get the current value
var something = appDelegate.getYourStuff();

// Or use a setter to set it, a modifier to modify oit
appDelegate.setYourStuff(yourStuff);
appDelegate.modifiyYourStuffAttribute(newAttributeValue); 

不知道这种方法是否是一种不好的做法,但它对我有用。

接受其他建议!

关于ios - 在 VIewController 之间共享数据 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33295198/

相关文章:

ios - 无法使用 Firebase 检索最近添加的值

IOS 推送通知深层链接?

java - 为什么有多个托管 Bean?

c++ - 什么时候在 C++11 lambda 的定义中需要明确命名的变量捕获?

ios - 从数组中选择 UIlabel 背景颜色,然后将其从数组中删除。 ( swift )

iphone - 主线程上的多个 GCD 调度

ios - 如何动画一个接一个的 UILabel

arrays - $0 和 $1 在 Swift 闭包中是什么意思?

ios - App在后台时,APNS token是否发生变化或失效?

VB.Net 属性 - 公共(public)获取、私有(private)设置