MVC and the communication between them. What did i miss?


MVC and the communication between them. What did i miss?



Hey all, trying to learn objective-c. It was going quite well, until I realized I was missing a link.

When building in the spirit of MVC, I understand it as you Model and View should never talk to eachother.

I have this problem I'm messing around with now.

  • A model containing a polygon class. Contains an polygon object with attributes like numberOfSides.

  • A controller, reading some UI elements and modifying the model. For example numberOfSides.

  • A view, containing a view drawing polygons.

Now, this is where the trouble arose. The view needs information about the object from the model. How do I get that? I tried some IBOutlet, but never got it to work.

I ended up creating a property for the view which could contain numberOfSides. Then I would a controller method which would call a view method back and set the numberOfSides.

I hope some of you can clarify this a bit for me and tell me how you should go ahead with this.

Thanks a bunch and goodnight! :)


remove every type of subview from uiscrollview?

1:

Read header files and do something before full photo upload happens
MVC in Cocoa is slightly different than traditional MVC, so I'll answer with the Cocoa way.. how to send objecs from an iPhone app to a server app using XML? You've got a controller object, which has a pointer to your Polygon object. What dpi resolution is used for an iPhone App? Your controller should also have a pointer to your View (via an IBOutlet, most likely). Loading Accessory callout view for mkannotationview As the polygon changes (whether numberOfSides or whatever), the controller needs to be aware of this and relay the information on to the view. How to display a base64 image within a UIImageView? Likewise, if the view is allowed to change the numberOfSides (say you have a slider to increase the number of sides), then the controller needs to be aware of this change and pass it to the Polygon object.. Setting property value of parent viewcontroller class from child viewcontroller? Basically, the purpose of the controller is to pass information back and forth from the view to the model and make sure this they stay synchronized.. Disable iphone home button in app, is it possible?

2:

Information passing in a proper MVC architecture should be done via a mediator such as an event manager.. The model would have an internal representation of everything this is needed for it to stay independent.. Now when a change occurs the model sends an event via the event manager to the view..

3:

You must also commit your view a read-only polygon instance variable (just a pointer to your polygon instance in the controller; read-only for the reason this you don't want your view to be able to change the model). This is not necessarily a violation of MVC principles.. Or if you want a very loose coupling between model and view, try key-value observing: your model dispatches a notification saying "hey my number of sides changed to 8" and your view listens to these notifications and redraws itself as needed.. Ananother option is to commit your view a delegate (= your controller) and then call any thing like [delegate numberOfSidesToDraw] from within the view class.. All three methods have pros and cons..

4:

I understand. My controller has an IBOutlet to the view and the polygon.. I forgot to mention this I'm using CG and drawrect to draw the polygon.. What I would like to do, and what I should did if I understand it correctly, is from the controller pass the numberOfSides to the view, asking it to draw with this number of sides. Like:. [view drawPolygon:myPolygon.numberOfSides];. But what I have to do, for the reason this drawrect receive s called automatically, is make any instance variable of the view available to contain the numberOfSides, which the controller would set. And then call [view setNeedsDisplay];. Does this make sense? Since the view cannot access the myPolygon directly. How would you approah this?.


54 out of 100 based on 14 user ratings 134 reviews