views:

57

answers:

1

Hi,

I am trying to keep close to the MVC approach to programming in an objective C application.

I have a model class and two View Controllers.

@interface Disc : NSObject {


NSString *discType;
NSNumber *capacity; }


@property (nonatomic,retain) NSString *discType;
@property (nonatomic,retain) NSNumber*capacity;

@implementation Disc

@synthesize discType,capacity;

Then for View Controller A

@interface DiscTypeViewController : SecondLevelViewController {

NSString *discTypeSub;
}

@property (nonatomic,retain) NSString *discTypeSub;
@end


@implementation DiscTypeViewController

@synthesize discTypeSub;

Now, I know I can access the members of the model (disc) class from View controller A

Disc *disc1 = [[Disc alloc]init];

[disc1 setDiscType:@"DVD"]; 

discTypeSub = [disc1 discType];

This returns the value "DVD" which is fine.

The question is, how can my Second View Controller access that same String that returned "DVD". There's no point in initializing a new instance of Disc. I need the values that were created from View Controller A calling the setter/getter methods of the Disc class.

What is the best design approach for such a scenario, any info would be much appreciated.

A: 

You could create some sort of a context object. Most likely it would be a SingleTon or Factory (static getters/setters). You can have a dictionary with keys @"DVD" or @"BlueRay" with values of type Disk. Please consider that these objects will reside in your memory while stored in you cache dictionary. Consider to use NSCache if you target iOS4.

bioffe
Thanks bioffe. I'll explore the singleton route as suggested.
The Singleton pattern is complex and overkill for the given question. Please see my answer for something that is much easier to support.
Philip Regan