我应该在何处以及如何实例化将在IOS应用程序中全局使用的对象? [英] Where and how should I instantiate an object which will be used globally in an IOS app?

查看:99
本文介绍了我应该在何处以及如何实例化将在IOS应用程序中全局使用的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,我想说我正在为ios5制作秒表应用程序。我有一个名为秒表的类,它代表秒表的状态。这个类拥有存储已用时间的成员,秒表是否正在运行等等。

For simplicity's sake, lets say I am making a stopwatch application for ios5. I have a class called stopwatch, which represents the stopwatch's state. This class possesses members that store elapsed time, whether the stopwatch is running, etc.

对我来说,似乎这个类应该在我的应用程序生命周期开始时实例化一次,因此我声明它是AppDelegate类的成员,并在AppDelegate类的didFinishLaunchingWithOptions:方法中实例化它

To me, it seems this class should be instantiated once at the beginning of my apps lifetime, thus I declare it to be a member of the AppDelegate class and instantiate it in the didFinishLaunchingWithOptions: method of the AppDelegate class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:     (NSDictionary *)launchOptions
{
    Stopwatch *stopwatch = [[Stopwatch alloc] init];
    return YES;
}

然而,我现在发现自己需要能够引用这个秒表对象了ViewController类,但不知道如何操作。

However, I now find myself needing to be able to reference this stopwatch object in the ViewController class but not knowing how to do so.

我非常感谢有关如何处理这个问题的任何建议,因为它似乎是一个基本的制作概念ios应用程序。谢谢!

I would greatly appreciate any advice as to how to approach this problem, as it seems to be a fundamental concept for making ios applications. Thank you!

推荐答案


  1. 不要使用单身人士。在大多数情况下,单身人士做出糟糕的设计决定有很多原因,我写了 a关于其中一些的博客文章

不要在应用代理中存储任意数据。这只是滥用现有的单身人士做你想做的事情。应用程序代表应关注应用程序的生命周期事件,它不应该是您想要从任何地方访问任何数据的地方。

Don’t store arbitrary data in the app delegate. That’s just misusing an existing singleton to do what you want. The app delegate should be concerned with the app’s lifecycle events, it should not be a place to stick whatever data you want accessible from everywhere.

解决此问题的一种可能的正确方法是创建一个类,为您创建应用程序的对象图,并将实例传递给所有感兴趣的类。我的app delegate的应用程序确实完成了启动方法,大部分看起来像这样:

One possible correct approach to solve this problem is to have a class that will create you application’s object graph for you and pass the instances to all interested classes. My app delegate’s application did finish launching method mostly look like this:

- (BOOL) applicationDidFinishWhatever
{
    Factory *factory = [[Factory alloc] init];
    [self setWindow:[factory buildMainWindow]];
    [window makeKeyAndSomething];
    return YES;
}

所有对象创建和组装都在工厂类。 Factory将创建计时器(如果需要,将单个实例保留在内存中),它将通过计时器查看控制器:

And all the object creation and assembly gets done in the Factory class. The Factory will create the timer (and keep the single instance in memory, if needed) and it will pass the timer to view controllers:

- (void) buildMainWindow
{
    UIWindow *window = …;
    [window setRootViewController:[self buildMainController]];
    return window;
}

- (void) buildMainController
{
    UIViewController *controller = …;
    [controller setTimer:[self buildTimer]];
    return controller;
}

我创建了一个示例Xcode项目,展示如何在没有单身人士的情况下连接应用程序。现在非常简单,我稍后会添加一些常见的用例。

I have created a sample Xcode project that shows how to wire an app without singletons. It’s very simple right now, I will add some common use cases later.

这篇关于我应该在何处以及如何实例化将在IOS应用程序中全局使用的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆