Normally, of course, models shouldn't know anything about UI, and vice versa, 
but what about when the models themselves represent something visual that the 
UI needs to draw (like in a drawing/painting program, or - in my case - 
GameObject instances that can be objects found in the "dungeon" that the player 
in the game can see and possibly interact with)?

In this case, it makes sense that the object with the information needed to 
draw, is the model object itself (since it has all the properties needed, such 
as visible, passable, floorImage, etc...), but the model objects have no 
graphics context to draw into (that's the responsibility of the drawRect: 
method of a UIView subclass). So, in my app, the view's drawRect: method will 
get a reference to it's view controller, and ask for all the model objects that 
are visible within its bounds rect (and considering the "camera" view rect 
which is managed by the view controller), and will then iterate over them, 
asking them to draw themselves, now that the graphics context is properly set 
up.

Otherwise, the UIView subclass' drawRect: method becomes unwieldy as it is then 
too coupled to the model objects (i.e. it would - for each model object - have 
to determine what class they are, then have custom drawing code to actually 
draw what the model represents) This is too brittle, and is one reason to use 
polymorphism via subclassing to define behaviors specific to a particular class 
of object.

The question is, if my GameObject (base class) defines a drawRect(bounds: 
CGRect) itself, and the code in the UIView subclass' drawRect: method passes a 
CGRect (which is within it's own bounds) to the model's drawRect:, will the 
code in the model's drawRect: "see" the graphics context of the UIView, so that 
you can set the stroke or fill color using the setStroke() and setFill() 
methods on UIColor, as well as anything else that may require that context, 
such as lower-level calls to CoreGraphics?


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to