سؤال

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;
}
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

If you want to change the image on click following code might be useful. Otherwise if you want to change them dynamically use the code in Action method with NSTimer delegate method.

Drag and drop some images in bundle and pass their names to array.

singleton .h file
#import <Foundation/Foundation.h>

@interface SingleTon : NSObject
{

}

+(SingleTon *) createSingleTon;

-(NSString *) returnImage:(NSString *) imageIs;
@end



singleton .m file
@implementation SingleTon

+(SingleTon *) createSingleTon
{
    static SingleTon *single= nil;
    if (single == nil) {

        single = [[SingleTon alloc] init];
    }
    return single;
}

-(NSString *) returnImage:(NSString  *) imageIs
{
    return imageIs;
}
@end



 viewcontroller .h file
    #import <UIKit/UIKit.h>
    #import "SingleTon.h"
    @interface ViewController : UIViewController<UITextFieldDelegate>
    {
        SingleTon *sing;
        IBOutlet UIImageView *displayImage;
       // connect this outlet imageview in xib.
        NSArray *imagesArray;
        int i;

    }
    -(IBAction)changeImage:(id)sender;
    @end

viewcontroller .m file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    imagesArray = [[NSArray alloc] initWithObjects:@"images.jpg",@"images1.jpg",@"images2.jpg",@"images4.jpg", nil];
    i = 0;
    sing = [SingleTon createSingleTon];
    // Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)changeImage:(id)sender
{

    if (i<[imagesArray count]) {
        displayImage.image = [UIImage imageNamed:[sing returnImage:[imagesArray objectAtIndex:i]]];

    }
    i++;


}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top