I'm developing for the iPhone, and I have a class DataManager
, that is used to maintain my application data. When the application launches/exits, the data is read from/written to disk to create an instance of this class, using the NSKeyedArchiver (and Unarchiver) classes, since the DataManager adheres to the NSCoding protocol.
我正在为iPhone开发,我有一个类DataManager,用于维护我的应用程序数据。当应用程序启动/退出时,使用NSKeyedArchiver(和Unarchiver)类从磁盘读取/写入数据以创建此类的实例,因为DataManager遵循NSCoding协议。
One problem I'm having is that I need the DataManager to be accessible by many of my other IB classes, so it is defined as an object in IB, and those classes have an outlet to it. The DataManager is being created using the standard init: method (or maybe initWithCoder:?), but since IB doesn't have the proper file (or NSData from the file) to instantiate the object, it has no initial contents.
我遇到的一个问题是我需要DataManager可以被我的许多其他IB类访问,因此它被定义为IB中的一个对象,并且这些类有一个插座。 DataManager是使用标准的init:方法(或者可能是initWithCoder:?)创建的,但由于IB没有正确的文件(或文件中的NSData)来实例化对象,因此它没有初始内容。
So, is there any way to tell IB not to instantiate the class automatically? This will instead be performed using my application delegate, something like:
那么,有没有办法告诉IB不要自动实例化该类?这将使用我的应用程序委托来执行,例如:
AppDelegate.h
IBOutlet DataContext *context;
AppDelegate.m
context = [NSKeyedUnarchiver unarchiveObjectWithData:dataLoadedFromFile];
As you can see, this presents a problem. Wouldn't the context be instantiated twice, once by InterfaceBuilder, then a second time by my application delegate?
如您所见,这提出了一个问题。难道上下文不会被两次实例化,一次是InterfaceBuilder,第二次是我的应用程序委托?
I would like to prevent maintaining the context as an ivar in the delegate, since that seems to stray from the MVC paradigm, and leans toward the singleton pattern instead. (The controller should not be responsible for the data in my mind. It can maintain a reference to it obviously, but should not be responsible for offering it to other classes.)
我想阻止将上下文保持为委托中的ivar,因为这似乎偏离了MVC范例,而是倾向于单例模式。 (控制器不应该对我的数据负责。它可以显然保持对它的引用,但不应该负责将它提供给其他类。)
1 个解决方案
#1
When the application launches/exits, the data is read from/written to disk to create an instance of this class, using the NSKeyedArchiver (and Unarchiver) classes, since the DataManager adheres to the NSCoding protocol.
当应用程序启动/退出时,使用NSKeyedArchiver(和Unarchiver)类从磁盘读取/写入数据以创建此类的实例,因为DataManager遵循NSCoding协议。
One problem I'm having is that I need the DataManager to be accessible by 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 instantiated twice, once by InterfaceBuilder, then a second time by my application delegate?
我遇到的一个问题是我需要DataManager可以被我的许多其他IB类访问,所以它被定义为IB中的一个对象......正如你所看到的,这就产生了一个问题。难道上下文不会被两次实例化,一次是InterfaceBuilder,第二次是我的应用程序委托?
Yup.
First, you should think about whether this is a controller or a model object. It sounds to me like it's a controller.
首先,您应该考虑这是控制器还是模型对象。听起来我觉得它是一个控制器。
If it is, then you should move the model to a separate object or objects, and make those NSCoding-compliant, and make the data manager load and save those objects. A bonus of this solution is that you could tell the data manager to save the objects and purge them when you get a low-memory warning, not just at quit time.
如果是,则应将模型移动到单独的一个或多个对象,并使这些符合NSCoding,并使数据管理器加载并保存这些对象。此解决方案的一个好处是,您可以告诉数据管理器保存对象并在获得低内存警告时清除它们,而不仅仅是在退出时。
#1
When the application launches/exits, the data is read from/written to disk to create an instance of this class, using the NSKeyedArchiver (and Unarchiver) classes, since the DataManager adheres to the NSCoding protocol.
当应用程序启动/退出时,使用NSKeyedArchiver(和Unarchiver)类从磁盘读取/写入数据以创建此类的实例,因为DataManager遵循NSCoding协议。
One problem I'm having is that I need the DataManager to be accessible by 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 instantiated twice, once by InterfaceBuilder, then a second time by my application delegate?
我遇到的一个问题是我需要DataManager可以被我的许多其他IB类访问,所以它被定义为IB中的一个对象......正如你所看到的,这就产生了一个问题。难道上下文不会被两次实例化,一次是InterfaceBuilder,第二次是我的应用程序委托?
Yup.
First, you should think about whether this is a controller or a model object. It sounds to me like it's a controller.
首先,您应该考虑这是控制器还是模型对象。听起来我觉得它是一个控制器。
If it is, then you should move the model to a separate object or objects, and make those NSCoding-compliant, and make the data manager load and save those objects. A bonus of this solution is that you could tell the data manager to save the objects and purge them when you get a low-memory warning, not just at quit time.
如果是,则应将模型移动到单独的一个或多个对象,并使这些符合NSCoding,并使数据管理器加载并保存这些对象。此解决方案的一个好处是,您可以告诉数据管理器保存对象并在获得低内存警告时清除它们,而不仅仅是在退出时。