对于创建的任何对象,我通常使用两个两个作用域: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/