Finish creating an instance of the Builder interface?
I am developing on iPhone and I have a class DataManager
that is used to maintain my application data. When the application starts / exits, data is read from / written to disk to instantiate this class using the NSKeyedArchiver (and Unarchiver) classes, since the DataManager adheres to the NSCoding protocol.
One of the problems I am facing is that I need a DataManager to access many of my other IB classes, so it is defined as an object in IB and these classes have access to it. The DataManager is instantiated using the standard init: method (or perhaps initWithCoder :?), but since IB does not have the proper file (or NSData from file) to instantiate the object, it has no original content.
So, is there a way to tell IB not to instantiate the class automatically? Instead, it will be done with my application delegate, like this:
AppDelegate.h
IBOutlet DataContext *context;
AppDelegate.m
context = [NSKeyedUnarchiver unarchiveObjectWithData:dataLoadedFromFile];
As you can see, this presents a problem. Wouldn't the context be created twice, once through the InterfaceBuilder, and then a second time by the application delegate?
I would like to prevent the context from being saved as an ivar in the delegate as this seems to deviate from the MVC paradigm and relies on the singleton pattern instead. (The controller shouldn't be held responsible for the data in my mind. It can maintain a link to it, obviously, but shouldn't be held responsible for suggesting it to other classes.)
a source to share
When the application starts / exits, data is read from / written to disk to instantiate this class using the NSKeyedArchiver (and Unarchiver) classes, since the DataManager adheres to the NSCoding protocol.
One of the problems I am having is that I need a DataManager to access many of my other IB classes, so it is defined as an object in IB ... As you can see, this presents a problem. Wouldn't the context be created twice, once through the InterfaceBuilder, and then a second time by the application delegate?
Yes.
First, you have to think about whether it is a controller or a model object. It looks to me like a controller.
If so, then you should move the model to a separate object or objects and make them NSCoding compliant, and force the data manager to load and save those objects. The bonus of this solution is that you can tell the data manager to save the objects and clean them up when you get a low memory warning, not just when you exit.
a source to share