我试图与MVC在目标C应用程序中进行编程的方法保持紧密联系。
我有一个模型类和两个视图控制器。
@interface Disc : NSObject {
NSString *discType;
NSNumber *capacity; }
@property (nonatomic,retain) NSString *discType;
@property (nonatomic,retain) NSNumber*capacity;
@implementation Disc
@synthesize discType,capacity;
然后对于View Controller A
@interface DiscTypeViewController : SecondLevelViewController {
NSString *discTypeSub;
}
@property (nonatomic,retain) NSString *discTypeSub;
@end
@implementation DiscTypeViewController
@synthesize discTypeSub;
现在,我知道我可以从View控制器A访问模型(disc)类的成员
Disc *disc1 = [[Disc alloc]init];
[disc1 setDiscType:@"DVD"];
discTypeSub = [disc1 discType];
这将返回值“ DVD”,这很好。
问题是,我的第二个视图控制器如何访问返回的同一字符串
“DVD”。初始化Disc的新实例没有任何意义。我需要的是
从View Controller A创建,它调用Disc类的setter / getter方法。
对于这种情况最好的设计方法是什么,任何信息将不胜感激。
最佳答案
您可以创建某种上下文对象。最有可能是SingleTon或Factory(静态获取器/设置器)。您可以使用键类型为Disk的@“ DVD”或@“ BlueRay”字典。
请考虑这些对象在存储在缓存字典中时将驻留在内存中。如果您针对iOS4,请考虑使用NSCache。