ios - view as property of model object alternative -
i'm building arithmetic app & in there subclasses of nsobject
numbers , digits.both of these have corresponding view objects take datasource (either number or digit) , delegate, view controller.
i have found useful in getting views & model work set digit views property of corresponding digits.
for example, number class has nsmutablearray
property holds digits.
if want find size corresponding numberview, write can write code in controller:
-(void) updatenumberviewframesize:(acnumberview*) sender { nsuinteger i; float width = 0, height = 0; (acdigit* digit in [sender.datasource returnnumberviewdatasource].digitarray) { width += digit.digitview.size.width; height += digit.digitview.size.width; } sender.frame = cgrectmake(sender.frame.origin.x, sender.frame.origin.y, width, height); }
the code works fine, feel not practice hold pointer view model, if model isn't using itself.
if bad practice, potential pitfalls, , there better way achieve type end ?
first: right. no design.
second: calculate size of number view inside model. number view should know size itself. knows number through data source , can digits. has information calculating size.
to show problem, imagine (even situation on ios not common), display same number @ 2 places (= different number views). break model. why?
solution: put code related graphic state (drawing, size, …) number view , digit view. on half of way additional work. @ end, when every code migrated view layer, easy computing inside model.
Comments
Post a Comment