IPhone help with singleton class

Hi,

I am looking at a Matt Gallagher macro to create singleton classes. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

Basically, I have an application with several views, and I want to have access to the “global” data from each of these views using a singleton class.

I basically have three lines that I want to get in this class: NSString * uname, NSString * details and NSString * selectedDetails.

Do I need to create three singleton classes with a static variable in each?

Also, how do I get and set the string variables uname, details and selectedDetails?

I got a little mixed up with all this singleton stuff (today I met only with such things!), And I was wondering if anyone could point me in the right direction.

Thank you very much in advance,

Here is the code I made:

#import <Foundation/Foundation.h>

@interface Details : NSObject{

}
+(XXX *)sharedXXX;
@end


#import "Details.h"
#import "SynthesizeSingleton.h"

@implementation Details
SYNTHESIZE_SINGLETON_FOR_CLASS(XXX);
@end
0
source share
3 answers

Do I need to create three singleton classes with a static variable in each?

No. Just create one that has all three.

Also, how do I get and set the string variables uname, details and selectedDetails?

You get a link to your singleton, usually with something like the following:

MySingleton *singleton = [MySingleton sharedInstance];

Then you use it like any other object:

singleton.uname = @"Example";

Are you sure you really need a single? If this is user data, then how to store it with NSUserDefaults?

+1
source

, ...

//.h:
import <Foundation/Foundation.h>

@interface Details : NSObject {
    NSString *global_uname;
    NSString *global_details;
    NSString *global_selectedDetails;
}
@property (nonatomic,retain) NSString *global_uname,*global_details,*global_selectedDetails;

+(Details*) sharedDetails;
@end




//.m:
#import "Details.h"
#import "SynthesizeSingleton.h"

@implementation Details
SYNTHESIZE_SINGLETON_FOR_CLASS(Details);
@synthesize global_uname,global_details,global_selectedDetails;
@end

/ :

[Details sharedDetails].global_uname
+1
T1=[[UITextField alloc] init];
    T2=[[UITextField alloc] init];
    T3=[[UITextField alloc] init];
    T4=[[UITextField alloc] init];
    [T1  addTarget:self action:@selector(goto:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [T2  addTarget:self action:@selector(goto:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [T3  addTarget:self action:@selector(goto:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [T4  addTarget:self action:@selector(goto:) forControlEvents:UIControlEventEditingDidEndOnExit];

    T1.backgroundColor=[UIColor whiteColor];
    T2.backgroundColor=[UIColor whiteColor];
    T3.backgroundColor=[UIColor whiteColor];
    T4.backgroundColor=[UIColor whiteColor];
    T1.placeholder=@"companyname";
    T2.placeholder=@"address";
    T3.placeholder=@"contactno";
    T4.placeholder=@"noofemp";

    T1.frame=CGRectMake(60, 0, 100, 30);
    T2.frame=CGRectMake(60, 60, 100, 30);
    T3.frame=CGRectMake(60, 110, 100, 30);
    T4.frame=CGRectMake(60, 160, 100, 30);
-1

All Articles