Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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;
}

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.

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
700 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. Don’t use a singleton. There are plenty of reasons why singletons are a bad design decision in most cases, I wrote a blog post about some of them.

  2. 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.

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;
}

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;
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...