Re: Binding to a Singleton
Re: Binding to a Singleton
- Subject: Re: Binding to a Singleton
- From: Mike Abdullah <email@hidden>
- Date: Wed, 27 Sep 2006 23:12:27 +0100
As an experiment, I just created a plain NSDocument based app and
added an NSUserDefaultsController to the document nib.
When I launch it with ObjectAlloc, I can see that an instance of
NSUserDefaultsController is created for every document.
Would it then be a reasonable assumption that based on this I should
subclass NSObjectController to always attach itself to my Singleton
class? That way I will have my Singleton with multiple controllers
attached.
Mike.
On 26 Sep 2006, at 17:45, Shaun Wexler wrote:
On Sep 26, 2006, at 9:02 AM, Mike Abdullah wrote:
Because it's a singleton. I want to bind to it from my document
NIB, and by instantiating the class in the NIB will result in
multiple instances of my class. There will be one instance for
every document - not something I want!
In my app I have a singleton class that behaves kinda like
NSUserDefaults. So I'm wondering, what's my best bet for how to
go about binding to this class from a document NIB.
A nib file contains archived copies of your objects, which are
instantiated in the app using -initWithCoder: and that ultimately
calls -init on your object. Your singleton class should override
+allocWithZone: to prevent the creation of additional objects.
Your singleton instance can then intercept either of the init
methods as well as -awakeFromNib, and deal with the nib
accordingly. You do not want to use the singleton as File's Owner
for outlet or binding purposes though.
Another way is to declare a category and bind to
NSApplication.mySingleton:
static MySingletonClass *mySingletonSharedInstance;
@interface NSApplication (MySingletonClass)
- (MySingletonClass)mySingleton;
@end
@implementation NSApplication (MySingletonClass)
+ (void)load
{
[MySingletonClass class]; // +initialize if necessary
}
- (MySingletonClass)mySingleton
{
return mySingletonSharedInstance;
}
@end
@implementation MySingletonClass
+ (void)initialize
{
if (self == [MySingletonClass class]) {
mySingletonSharedInstance = [[super allocWithZone:NULL] init];
}
}
+ (id)allocWithZone:(NSZone *)zone
{
return nil;
}
@end
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden