Search code examples
objective-cios4singletonthemesios-4.2

How to use singleton in iphone app?


hello all m a newbie to iphone development.

I have 5 themes in my application.

I have static images for every theme(for labels n buttons and all), but i dont know how i can apply or change new theme to whole application. I read through singleton but cant understand how can i use it to change images of whole application.

Is there any other way to apply theme to application or is the singleton is the only method, if yes, then how can i use it.

EDIT: This is my code of singleton class but i cant found any way to implement images for various themes.

#import "MyClass.h"




@implementation MyClass

+ (MyClass *)sharedInstance
{
    static MyClass *instance;
    @synchronized(self)
    {
    if(!instance)
    {
        instance = [[MyClass alloc] init];
    }
    }

return instance;
}

Solution

  • You should look into the UIAppearance proxy in iOS 5. For earlier versions the singleton approach seems fine, you request an appearance change to the singleton which changes its various images, then either sends a notification (NSNotification) to inform any interested parties who would want to update their interface accordingly. Or you can set key value observers (KVO) on the singleton properties so they are automatically informed when one of them changes.

    This post shows you how to implement a singleton. It's just a way to be able to access the same instance of a class from anywhere. Here is a link to a discussion about the singleton design pattern along with a more involved "pure" singleton implementation.