一直在研究如何将数据从一个子视图发送到另一个子视图。该应用程序有多个视图,其中一个是实时设置视图。设置的更改应该会影响其他视图中的内容。
NSUserDefaults 似乎很流行,但我不想要持久数据,也不想要自动保存到数据库/文件中。看起来像是潜在的放缓,我想避免这种情况。
第二个流行的事情是将一些常见的顶级对象的引用传递到每个子视图中。只是还没有找到任何教程,可以详细地向我展示如何做到这一点...很可能很简单,不需要教程 - 在你弄清楚一次之后!如果您能给我指出正确的方向(URL),我将不胜感激!
问题:我正在考虑使用一个“单一对象”来存储“全局数据”,以便随时随地访问它。这有什么问题吗?如果我在例如 appDelegate.m 创建它,它应该在任何人尝试访问它之前就存在,对吧?
我正在查看 此示例代码。
Been researching how to send data from one child view to another child view. The application has several views, where one is kind of real-time Settings view. Changes in settings should effect stuff inside other views.
NSUserDefaults seems to be popular, but I don't want persistent data nor automatic saving into database/file. Looks like potential slowdown, which I want to avoid.
Second popular thing is passing a reference to some common top level object into each child view. Just haven't found any tutorial, which would show me in detail how to do this... Most likely so easy, no tutorial is needed - after you figure it out once! Would appreciate, if you can point me to right direction (URL)!
Question: I'm thinking about using one "singleton object" to store "global data", access it from everywhere anytime. What could wrong with this? If I create it at e.g. appDelegate.m it should exist before anyone tries to access it, right?
I'm looking at this sample code.
发布评论
评论(2)
我建议使用委托和/或通知。
不要在应用程序委托中创建新的单例,而是在应用程序委托中添加必要的数据(或者更好的是对数据的引用),并使视图引用应用程序委托。
您还可以设置 通知,以便您的视图可以跟踪其显示的数据的更改。
I would recommend using delegates and/or notifications.
Instead of creating a new singleton in the app delegate, add the necessary data (or, even better, references to the data) in the app delegate and have the views have references to the app delegate.
You can also set up notifications so that your views can keep track of changes to the data that they display.
单例的美妙之处在于,当您第一次通过某些[singletonClass共享实例]访问它时,它会自动创建。所以你不需要在启动时“创建”它。如果需要从任何视图访问全局数据,单例可能是执行此操作的正确方法。
The beauty of a singleton is that it is automatically created when you first access it via some [singletonClass sharedInstance]. So you don't need to "create" it while launching. If it is global data that needs to be accessed from any view singleton might be the right way of doing this.