Observing MVC, can/should the Model be instantiated in the ViewController? Or where?
Posted
by
user19410
on Programmers
See other posts from Programmers
or by user19410
Published on 2011-03-06T10:03:35Z
Indexed on
2012/10/26
23:17 UTC
Read the original article
Hit count: 149
I'm writing an experimental iPhone app to learn about the MVC paradigm. I instantiate my Model class in the ViewController class. Is this stupid? I'm asking because storing the id of the Model class, and using it works where it's initialized, but referring to it later (in response to an interface action) crashes.
Seemingly, the pointer address of my Model class instance changes, but how can that be?
The code in question:
@interface Soundcheck_Tone_GeneratorViewController : UIViewController {
IBOutlet UIPickerView * frequencyWheel;
@public
Sinewave_Generation * sineGenerator;
}
@property(nonatomic,retain) Sinewave_Generation * sineGenerator;
@end
@implementation Soundcheck_Tone_GeneratorViewController
@synthesize sineGenerator;
- (void)viewDidLoad {
[super viewDidLoad];
[self setSineGenerator:[[Sinewave_Generation alloc] initWithFrequency:20.0]]; // using reference -> fine
}
// pickerView handling is omitted here...
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[[self sineGenerator] setFrequency:20.0]; // using reference -> crash
}
@end
// the Sinewave_Generation class... only to be thorough. Works fine so far.
@interface Sinewave_Generation : NSObject {
AudioComponentInstance toneUnit;
@public
double frequency,theta;
}
@property double frequency;
- (Sinewave_Generation *) initWithFrequency: (int) f;
@end
@implementation Sinewave_Generation
@synthesize frequency;
- (Sinewave_Generation *) initWithFrequency: (int) f
{
self = [super init];
if ( self ) {
[self setFrequency: f];
}
return self;
}
@end
© Programmers or respective owner