Re: Binding to a Singleton
Re: Binding to a Singleton
- Subject: Re: Binding to a Singleton
- From: Shaun Wexler <email@hidden>
- Date: Tue, 26 Sep 2006 09:45:17 -0700
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