Have you ever wanted to share data between views, but couldn't figure it out for the life of you? Now, here's a solution. A singleton class. Any class can generate a reference (pointer) to the shared version of the class, which is only allocated once. You can then set values and retreive them, or write up any class methods you'd like.
Basically, instead of calling
myclass *instance = [[myclass alloc] init];
every time you want to reference a class (and bear in mind, it's a *new* instance of that class, so if you add variables to it the next time you allocate one of those classes it won't have the variables)
You'd call
myclass *instance = [myclass sharedInstance];
Then, any modifications to that instance change the entire thing, meaning that should you get another reference to +sharedInstance later it would still have the variables from before, because, in essence, it's the same instance! Should you want to share data, all you'd have to do is add a variable to the singleton's interface, such as an NSString.
NSString *myVariable
Then, you'd declare properties for it and synthesize it as usuaul.
From your other class, you'd include the header file for the singleton class (#import "singleton.h")
And then you'd just get a reference to the shared instance (using my example method below to create the instance),
and then change variables. Then, from another class, you can get a reference to the shared instance and look at what those variables are.
Here's a small example
// Singleton.h
@interface Singleton : NSObject
{
NSMutableDictionary *keys;
}
@property (nonatomic, retain) NSMutableDictionary *keys;
+ (Singleton *)sharedSingleton;
As you can see, we have one "class" method called "sharedSingleton". It is a class method because it starts with a plus sign, instead of the usual minus sign. One thing about class methods is that they normally return autoreleased objects. In iPhone programming you've seen this in:
[NSUserDefaults standardUserDefaults]
[UIDevice currentDevice]
[UIScreen mainScreen]
etc.
If you make a pointer to one of them, it's automatically released (autorelease!)
Now, to our Singleton.m file
// singleton.m
static Singleton *shared = NULL;
@implementation Singleton
- (id)init
{
if ( self = [super init] )
{
self.keys = [[NSMutableDictionary alloc] init];
}
return self;
}
+ (Singleton *)sharedSingleton
{
@synchronize shared
{
if ( !shared || shared == NULL )
{
// allocate the shared instance, because it hasn't been done yet
shared = [[Singleton alloc] init];
}
return shared;
}
}
- (void)dealloc
{
NSLog(@\"Deallocating singleton...\");
[keys release];
[super dealloc];
}
@end
As you can see, we allocate a new "sharedInstance" if the current one is null.
Now, we just expose the keys as a property, and you basically have your own NSUserDefaults (a way of interacting between views).
Say, you have a variable from one view that you want to pass to another.
ie: score = @"500";
in the first view, you'd have to import singleton.h
then you'd say
Singleton *singleton = [Singleton sharedSingleton];
[singleton.keys setObject:score forKey:@"score"];
in your other view, you'd do this, only you'd use objectForKey on the keys property
anybody, feel free to post and correct something, as i've only used singleton's a few times (well custom ones atleast)
Replies
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeIt's better to create your own singelton...
Ummm by the way shouldn't you use @synchronize?
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeTotal newbie at programming in general, but understand basic concepts.
How would you set up an array/sub-arrays of data, and pass one of the items in an array (or one of the items in a sub-array) to another class?
What about SQLite?
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeYou should add @synchronize { } around the references to our actual singleton allocation, in case you have to reference it from different threads.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeThe "@synchronize shared" in the .m file should read "@synchronized (shared)".
The reference in the description at the end about "objectForKey" should read "valueForKey".
Thanks for the insight, the work and the coding sample. Works great!
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeThe dispatch once token and block ensure that the singleton is only allocated once.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeRegistry pattern in Cocoa cocos2d for iPhone
You should follow Drop Dem on Twitter <a href="http://twit
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome