Making an object accessible by both MyDocument AND a custom class
Making an object accessible by both MyDocument AND a custom class
- Subject: Making an object accessible by both MyDocument AND a custom class
- From: Andrew Garber <email@hidden>
- Date: Tue, 24 Sep 2002 18:44:59 -0700
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?
I came up with a solution, but it seems like kind of a hack:
Have the instance of MyCustomObject send out a notification when its
awakeFromNib method is called. The notification's object should be a
pointer to the instance of MyCustomObject. Make both MyDocument and
MidiPanelController obeservers of this notification. When they receive
the notification, have them copy the pointer like so:
// make this declaration in the MyDocument.h and MyPanelController.h
// MyCustomObject *pointerToCustomObject;
- (void)handleNotification:(NSNotification *)note
{
pointerToCustomObject = [note object];
}
Now they can message the single instance of MyCustomObject (without
having to create their own instance of MyCustomObject).
Is there a cleaner way to accomplish this?
How can I allow objects to EASILY communicate with the single instance
of MyCustomObject?
Any help would be appreciated :)
_______________________________________________
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.