Re: Making an object accessible by both MyDocument AND a custom class
Re: Making an object accessible by both MyDocument AND a custom class
- Subject: Re: Making an object accessible by both MyDocument AND a custom class
- From: Eric <email@hidden>
- Date: Wed, 25 Sep 2002 02:14:35 -0400
on 9/24/02 9:44 PM, Andrew Garber at email@hidden wrote:
>
Hi! I'm writing a cocoa document-based app. I have a subclass of
>
NSDocument called MyDocument (as Project Builder normally provides),
>
and a subclass of NSObject called MyCustomObject. I also have an
>
instance of an NSPanel in my MainMenu.nib which is connected to an
>
instance of MyPanelController (a subclass of NSWindowController). I
>
only want MyCustomObject object to be instantiated once. I want
>
instances of MyDocument to be able to message the instance of
>
MyCustomObject (without creating new instances of it). I also want the
>
instance of MyPanelController to be able to message the instance of
>
MyCustomObject.
>
>
How can I allow objects to communicate with the single instance of
>
MyCustomObject?
Hi Andrew,
I think what you want here is a "shared instance" method for your
MyCustomObject class. It would go something like this:
+ (MyCustomObject *) sharedMyCustomObject
{
static MyCustomObject *sharedMyCustomObject = nil;
if (sharedMyCustomObject == nil)
{
sharedMyCustomObject = [[MyCustomObject alloc] init];
}
return sharedMyCustomObject;
}
Note that the method is a class method, so whenever you need the shared
MyCustomObject instance, you would call the method like so: [MyCustomObject
sharedMyCustomObject]. This avoids instantiating multiple instances of
MyCustomObject.
Hope this helps,
Eric Wang
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.