Singlton Class Example With Single Alert declaration Through out whole application

Hello Friends going to Show you how to make singleton class and declare one time UIALertView into singlton class and use through out application without declaring again and its a customize alertview. 

/*Signlton.h starts*/

#import <Foundation/Foundation.h>

void ALERT(NSString *title, NSString *message,NSString *canceBtnTitle,id delegate,NSString *otherButtonTitles, … );

@interface Signlton : NSObject

{

NSMutableArray *contentArray;

}

– (NSMutableArray *) getArray;

– (void) addArray:(NSMutableArray *)mutableArray;

+(id) instance;

+(id) sharedInstance;

+(id) singleton;

@end

/*Signlton.h ends*/

/*Signlton.m starts*/

#import “Signlton.h”

@implementation Signlton

static NSMutableDictionary* _children;

void ALERT(NSString *title, NSString *message,NSString *canceBtnTitle,id delegate,NSString *otherButtonTitles, … )

{

UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title

message:message

delegate:delegate

cancelButtonTitle:canceBtnTitle

otherButtonTitles:nil

];

va_list args;

va_start(args, otherButtonTitles);

NSString *obj;

for (obj = otherButtonTitles; obj != nil; obj = va_arg(args, NSString*))

[alertView addButtonWithTitle:obj];

va_end(args);

[alertView show];

[alertView release];

}

+(void) initialize //thread-safe

{

if(!_children) {

_children = [[NSMutableDictionary alloc] init];

}

[_children setObject:[[self alloc] init] forKey:NSStringFromClass([self class])];

}

+(id) alloc {

id c;

if((c = [self instance])) {

return c;

}

return [self allocWithZone:nil];

}

-(id) init {

id c;

if((c = [_children objectForKey:NSStringFromClass([self class])])) { //sic, unfactored

return c;

}

if(self = [super init]){

contentArray = [[NSMutableArray alloc] init];

}

return self;

}

+(id) instance {

return [_children objectForKey:NSStringFromClass([self class])];

}

+(id) sharedInstance { //alias for instance

return [self instance];

}

+(id) singleton {      //alias for instance

return [self instance];

}

– (NSMutableArray *) getArray{

return contentArray;

}

– (void) addArray:(NSMutableArray *)mutableArray{

[contentArray addObject:mutableArray];

}

//stop other creative stuff

+(id) new {

return [self instance];

}

+(id)copyWithZone:(NSZone *)zone {

return [self instance];

}

+(id)mutableCopyWithZone:(NSZone *)zone {

return [self instance];

}

@end

/*Signlton.m ends*/

/* Now How to use this in your class */

/*ViewController.m starts*/

#import “ViewController.h”

#import “Signlton.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

[super viewDidLoad];

// if you want to deal with array then and then only use below code

Content *content = [Content sharedInstance];// this is not neccesory for UIAlertView method

[content addArray:mArray];

NSLog(@”contentArray: %@”, [content getArray]);

}

-(IBAction)CallAlert

{

ALERT(@”Message”, @”This church has been set as My Church.”, @”OK”, self, nil);

}

– (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

/*ViewController.m ends*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s