When programming for the iPhone, I find that I often need to use the same instance of an object in multiple views. What is the best way to handle this? My strategy so far has been to create it as a member of the root view and pass it to subsequent views which retain it as a member. However, this does not seem like a very good approach since it would be very difficult to update what the reference in each class is pointing to. I have also seen singleton classes as well as the AppDelegate used for this purpose. As an iPhone developer, how do you handle this problem in large apps?
在为iPhone编程时,我发现我经常需要在多个视图中使用相同的对象实例。处理这个问题的最佳方法是什么?到目前为止,我的策略是将其创建为根视图的成员,并将其传递给后续视图,后者将其保留为成员。但是,这似乎不是一个非常好的方法,因为更新每个类中的引用指向的内容非常困难。我也看过单例类以及用于此目的的AppDelegate。作为iPhone开发人员,您如何在大型应用程序中处理此问题?
1 个解决方案
#1
The App Delegate approach is easy to do, the downside is that everything has to go fetch the delegate and then get the shared object.
App Delegate方法很容易做到,缺点是一切都必须获取委托然后获取共享对象。
Singletons mean only classes using the singleton have to know about it, but can be harder to write unit tests against or clean up properly in low memory situations. Also you must write the singleton classes more carefully so they work correctly (look at Apple docs on singletons).
单例表示只使用单例的类必须知道它,但在低内存情况下编写单元测试或正确清理可能更困难。此外,您必须更仔细地编写单例类,以便它们正常工作(查看关于单例的Apple文档)。
Passing an object around can get old because sometimes you end up only having a link to the object to pass to someone else, so I would avoid that approach unless you were just passing from one parent to a few immediate children.
传递一个物体可能会变老,因为有时你最终只有一个物体的链接传递给其他人,所以我会避免这种方法,除非你只是从一个父母传给一些直接的孩子。
Between the first two, I lean a little towards singletons just because of the dependency graph being simpler (as mentioned classes only including singleton headers they care about instead of a whole bunch from the App Delegate). If you had a number of them the thing to do would be to make singleton distribution classes that held onto single instances of groups of classes, just to keep the app delegate lighter and not to have to make so many classes that were true singletons.
在前两个之间,我只是因为依赖图更简单(因为所提到的类只包括他们关心的单例头而不是来自App Delegate的整个群)而倾向于单身。如果你有很多这样的事情要做的事情是将单个分发类保存到类的组的单个实例上,只是为了让app委托变得更轻,而不是必须创建那么多真正的单例类。
I do like using the app delegate to hold on to the root of your actual UI, like the tab bar controller or primary view controller. That just seems more natural than stuffing it elsewhere.
我喜欢使用app delegate来保持实际UI的根,比如标签栏控制器或主视图控制器。这似乎比把它塞进其他地方更自然。
#1
The App Delegate approach is easy to do, the downside is that everything has to go fetch the delegate and then get the shared object.
App Delegate方法很容易做到,缺点是一切都必须获取委托然后获取共享对象。
Singletons mean only classes using the singleton have to know about it, but can be harder to write unit tests against or clean up properly in low memory situations. Also you must write the singleton classes more carefully so they work correctly (look at Apple docs on singletons).
单例表示只使用单例的类必须知道它,但在低内存情况下编写单元测试或正确清理可能更困难。此外,您必须更仔细地编写单例类,以便它们正常工作(查看关于单例的Apple文档)。
Passing an object around can get old because sometimes you end up only having a link to the object to pass to someone else, so I would avoid that approach unless you were just passing from one parent to a few immediate children.
传递一个物体可能会变老,因为有时你最终只有一个物体的链接传递给其他人,所以我会避免这种方法,除非你只是从一个父母传给一些直接的孩子。
Between the first two, I lean a little towards singletons just because of the dependency graph being simpler (as mentioned classes only including singleton headers they care about instead of a whole bunch from the App Delegate). If you had a number of them the thing to do would be to make singleton distribution classes that held onto single instances of groups of classes, just to keep the app delegate lighter and not to have to make so many classes that were true singletons.
在前两个之间,我只是因为依赖图更简单(因为所提到的类只包括他们关心的单例头而不是来自App Delegate的整个群)而倾向于单身。如果你有很多这样的事情要做的事情是将单个分发类保存到类的组的单个实例上,只是为了让app委托变得更轻,而不是必须创建那么多真正的单例类。
I do like using the app delegate to hold on to the root of your actual UI, like the tab bar controller or primary view controller. That just seems more natural than stuffing it elsewhere.
我喜欢使用app delegate来保持实际UI的根,比如标签栏控制器或主视图控制器。这似乎比把它塞进其他地方更自然。