Re: Loading data
Re: Loading data
- Subject: Re: Loading data
- From: j o a r <email@hidden>
- Date: Tue, 30 Jul 2002 23:42:58 +0200
I would create a shared data source object that could be accessed from
all the window controllers. Something like this (hacked in Mail, so you
need to check it!). Assuming that you have a class representing your
elements named "Element" it could look something like this:
@interface MyDataSource : NSObject
+ (Element *) doElement:(int) elementNumber;
@end
@implementation MyDataSource
+ (NSDictionary *) elementsFromDataFile
{
// Add code to parse the data from file and return it as a dicitonary
// or whatever you use as a container for your elements
}
+ (Element *) elementForName:(NSString *) element
{
static NSDictionary *elements = nil;
if (elements == nil)
{
elements = [[self elementsFromDataFile] retain];
}
return [elements objectForKey: element]];
}
@end
And then from your window controllers:
- (void) doElement:(NSString *) elementName
{
Element *element = [MyDataSource elementForName: elementName];
// Add the code to display the element information or whatever you
// you need to do here
}
This code takes advantage of the fact that you don't need to have any
connections between the window controller class and the data source
class. As long as you include the header for the data source class in
the window controller implementation you can refer to, and fetch data
from, the data source. You could add methods to the data source to also
allow for updates to the data if that is a requirement - like if
someone discovers a new element... :)
j o a r
On Tuesday, Jul 30, 2002, at 21:28 Europe/Stockholm, Andrew Merenbach
wrote:
Currently, I'm loading the element data in one controller, but I would
like doElement: to access the data in all of my different window
controllers. Is there a way to tell my other controllers to use the
doElement: method from one particular controller, or will I need to
load the data file four or five different times to do what I want?
_______________________________________________
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.
References: | |
| >Loading data (From: Andrew Merenbach <email@hidden>) |