开发者

iPhone help with singleton class

开发者 https://www.devze.com 2023-02-11 18:54 出处:网络
Greetings, I\'m looking at Matt Gallagher\'s macro for creating singleton classes. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

Greetings,

I'm looking at Matt Gallagher's macro for creating singleton classes. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

Basically, I have an app with multiple views and I want to be able to access "global" data from each of these views using a singleton class.

I basically have three strings I want to access in this class: NSString *uname, NSString *details and NSString *selectedDetails.

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

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

I'm a bit mixed up with all this singleton stuff (I only encountered such things today!) and I was wondering if anyone could poin开发者_运维百科t me in the right direction.

Many thanks in advance,

Here is some code I've done:

#import <Foundation/Foundation.h>

@interface Details : NSObject{

}
+(XXX *)sharedXXX;
@end


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

@implementation Details
SYNTHESIZE_SINGLETON_FOR_CLASS(XXX);
@end


Do I need to make 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 reference to your singleton, usually with something like the following:

MySingleton *singleton = [MySingleton sharedInstance];

Then you use it as you would any other object:

singleton.uname = @"Example";

Are you sure you really need a singleton though? If it's user data, what about storing it with NSUserDefaults?


Ok, I managed to solve this problem myself with the help of Jim...

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

And then you get/set using:

[Details sharedDetails].global_uname


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消