Re: Subclassing NSControl
Re: Subclassing NSControl
- Subject: Re: Subclassing NSControl
- From: David Remahl <email@hidden>
- Date: Wed, 14 May 2003 10:24:46 +0200
On Wednesday, May 14, 2003, at 10:14 AM, Scott Anguish wrote:
On Wednesday, May 14, 2003, at 03:51 AM, Lorenzo wrote:
Hi,
I have two own subclasses:
MYTextField subclass of NSTextField
MYImageView subclass of NSImageView
For many of their behaviours these 2 classes work the same way,
e.g. they respond to the mouse clicks the same way,
if the user clicks holding down the Control key, a pop-up menu
appears...
So I thought to group these 2 subclasses under a superClass "MYBox",
and to describe the common behaviours (like mouseDown, mouseUp)
in this new superClass "MYBox".
You can't really do that. NSTextField and NSImageView both inherit
from NSControl.
what you could do is have your own subclasses of NSTextField and
NSImageView and then have the mouseDown: and mouseUp: methods in your
subclasses call a procedure (yep, an old fashion procedure) passing
the sender and it's parameters
void CommonMouseDownHandler(NSEvent *theEvent, NSControl *sender)
void CommonMouseUpHandler(NSEvent *theEvent, NSControl *sender)
this would let you put all the code in a single shared location, and
still give you all the objective-C goodness of calling the objects..
You could even use class methods and make it into a class if you
wanted to (but I don't see the advantage in this case... maybe if you
had a single set of shared data that they needed to access a singleton
might make sense)
I was just typing up a response to the same effect as yours...
One advantage of using a separate class with class or instance methods
could be that you want to be able to change the set of common behaviors
dynamically at run-time. Using a class, referenced by an instance
variable in MYTextField would allow you simply to do a
-setCommonBehaviorClass: on the text field instance and have it shift
behaviors.
But I agree with Scott that in this case it probably isn't needed.
/ Rgds, David
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.