質問

I am working my way through the Stanford Fall 2011 iOS course: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/

I am onto assignment#3: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/system/files/assignments/Assignment%203_2.pdf

As a summary, prior assignments have asked to build a regular calculator and now this assignment is asking us to push this calculator unto a Nav controller and create a segue from that CalculatorViewController to a GraphViewController that would plot the function stored in the "CalculatorBrain". This CalculatorBrain was the model for the original CalculatorViewController.

Hint#5 keeps on talking about the fact that now the model for the GraphViewController is different than the model of the CalculatorViewController and I can't figure out what he means by that.

The only way I have been able to build the new MVC is by creating a protocol in the GraphView (view) of the GraphViewController with an object called "dataSource" of type ID. And then in the GraphViewController: adopting that protocol, instantiating the GraphView and setting itself as the datasource:

-(void) setGraphView:(GraphView *)graphView
  {
  _graphView=graphView;   
  self.graphView.dataSource=self;
  }

And then in the original CalculatoViewController, using the prepareForSegue to pass the program to the GraphViewController:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if([segue.identifier isEqualToString:@"Graph"])
  {
    GraphViewController *myGraphViewController = segue.destinationViewController;
    myGraphViewController.myCalculator=self.myCalcBrain;
  }
}

So that seems to work fine. So if that works, that means that the GraphViewController's model is really the original Calculator Brain that he specifically said it is not!

I mean isn't the fact that, during the segue, I am assigning to the Graphviewcontroller calculator property the calculator model instance from the original CalculatorViewController and then using a protocol to return the Y value from the GraphViewController to the GraphView means that the model for the GraphViewController is really just the original CalculatorBrain model.

役に立ちましたか?

解決

I do believe the model is the "program", an NSArray object. It can't be the description of the program cause there is no easy way to compute the "y" value given a NSString. You will have to re-parse the NSString into a stack (NSArray). As such, isn't it more direct to have the model as an NSArray/program?

他のヒント

I hate to answer this for you, since that's what you are supposed to do, but I do believe you are over thinking it, so here's what he wants you to realize/do:

Your model will be a new class that inherits from the model from the original calculator. That's why he says you will only need to create the view and controller from scratch. You will have to create a model class too, but a lot of the necessary code already exists in the old calc model, so you just inherit that and add the necessary additional functionality.

Hope that helps, and glad to see that you are using those courses. They are a great free tool!

I struggled a little with this question too when I was doing that assignment. I came to the conclusion (rightly or wrongly) that the model is just a formula - which seems like the conclusion you've come to, which while not being the calculator brain class, is very closely related to it!

edit:

I've just gone back and looked at my calculator program (i'm currently on the flickr assignment, so haven't looked at it in a while)

Looking at the basic calculator, the model was a "calculatorbrain" object

in the hints of the assignment it says

Don’t get this Controller’s Model confused with your CalculatorViewController’s Model. THEY ARE DIFFERENT. And don’t over-think this. Your new Controller’s Model is near at hand!

the graphview controller's model I have is a program. As in the program as defined in the calculatorbrain - just a stack of operations (NSArray) rather than a calculatorbrain. I just passed the current program via the segue into the graphview controller.

To me it seems to fit the details of the hint - the program is close at hand when you segue to the graph, but it it's not the same as the model of the calculator.

The hint doesn't say that it's not related to the model though.

But I'm a bit of an iOS n00b too, so this is just my take!

The model should just be a NSString describing the "program" that you are going to graph. As in the program that Calculator Brain returns to you!

id program = [self.dataSource.brain program];

My Model was the "program" also!

@Lee Cjin Pheow, you are correct. As I have progressed through this course, this has become more apparent. So the model is just an NSArray holding the program. I think my confusion was because of a n00b assumption I made that the Model in an MVC has to be its own class/file and hence my search for a Model class/file. As I am progressing, I am seeing the professor have an MVC where the Model is really just a property for example that is sitting in the Controller. I just have to maintain the Yellow do not cross lines in my simple mind.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top