Chapter 1 - MVC


#1

Hi There,

With regards to MVC, does the way that you have the Model and View objects in the same file not go against the point of MVC?

Cheers,
Nick
http://myfirstiphoneapp.co.uk


#2

I’d think that as long as they are separate objects you would still be complying with the concept of MVC. This is purely a guess on my part, and I’d be interested in hearing the thoughts of anyone with a bit more experience than I have.


#3

I’m not sure what you mean. In this application, there are instances of NSString and NSMutableArray (model objects) and instances of UILabel, UIWindow and UIButton (view objects).

Controller objects, like QuizAppDelegate, use model and view objects to do their bidding.


#4

I am not sure if my initial thinking was wrong. Here is the interface section for this project:

[code]@interface QuizAppDelegate : NSObject
{
int currentQuestionIndex;

// The model objects
NSMutableArray *questions;
NSMutableArray *answers;

// The view objects
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

  • (IBAction) showQuestion: (id) sender;
  • (IBAction) showAnswer: (id) sender;

@end[/code]

My initial thinking was that because we have the model (array) and view (UILabel) objects wrapped up in the same object (QuizAppDelegate - which I thought was the controller) went against MVC.

Because the book I previously worked on (Learning iPhone Programming) would start the majority of projects with a View Based template instead of Window based here where you get separate controller .h and .m files, then separate .h and .m files for the model object has somewhat confused me.

Then I read Chapter 2 where you describe Possession as having four separate objects makes me even more confused with the above.

I hope I am making sense here, and am not sounding completely stupid :blush:

Cheers,
Nick
http://myfirstiphoneapp.co.uk


#5

Each object exists independently of one another. The controller object (QuizAppDelegate) doesn’t have model and view objects inside it, it has pointer instance variables that hold the address of model and view objects.

The question you have to ask yourself is: if the QuizAppDelegate didn’t have pointers to model and view objects, how would it send them messages? How would it take information from the model objects and place them in the view objects for display?

(This is a less a question about MVC than it is a question about understanding pointers and objects.)