Re: Challenge 18 in Hillegass Book
Re: Challenge 18 in Hillegass Book
- Subject: Re: Challenge 18 in Hillegass Book
- From: Graham Cox <email@hidden>
- Date: Sat, 9 Aug 2008 11:40:05 +1000
On 9 Aug 2008, at 8:55 am, James G. wrote:
My question is along the lines of the "proper" way to design the
classes. I'll have a custom view class that will do the drawing, and
the collection of ovals will be an iVar of the MyDocument class, so
that I can support de/coding and undo. What's the best way to make
the oval collection available for drawing from the document class to
the custom view (and the reverse, to pass new ovals drawn by the
user from the view to the document for storage).
A couple of pointers that might be of help:
[NSArray makeObjectsPerformSelector:];
If your objects are NSBezierPaths, you could do this to draw the lot:
[myListOfPaths makeObjectsPerformSelector:@selector(fill)];
However bezier paths do not have information about colour or whether
to stroke, fill or both, so you might wrap your paths in another
object that adds those properties. That object might implement a
method such as -draw which you can supply as the argument above.
For undo, implement two methods - one to add an object to the list and
one to remove it. Then in the add method you'd do something like:
(typed into mail)
- (void) addMyObject:(id) object
{
[[[self undoManager] prepareWithInvocationTarget:self]
removeMyObject:object];
[myListOfPaths addObject:object];
[self setNeedsDisplay];
}
and the remove method:
- (void) removeMyObject:(id) object
{
[[[self undoManager] prepareWithInvocationTarget:self]
addMyObject:object];
[myListOfPaths removeObject:object];
[self setNeedsDisplay];
}
For simplicity these could be methods of your custom view class, but
equally they could live in a controller or model class elsewhere -
you'd then arrange to get the list from the model and draw it, or else
have a draw method in the model that you call from the view. So there
needs to be a connection between model, controller and view that
allows you to communicate freely back and forth. For now you could
just use the view as the model, unless you are really keen to explore
MVC at this point rather than just complete the exercise.
hth,
Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden