Re: Customizing controls: Inheritance bad?
Re: Customizing controls: Inheritance bad?
- Subject: Re: Customizing controls: Inheritance bad?
- From: Andy Lee <email@hidden>
- Date: Thu, 30 Oct 2008 13:54:55 -0400
On Oct 30, 2008, at 11:51 AM, Brian Williams wrote:
I've heard that, in general, if you are using inheritance in Cocoa,
you're not following the typical standard design pattern. Is this
true?
I would say that's an overstatement. Many Cocoa classes are designed
so that you should only need to subclass them rarely, and often with
fairly minor modifications to the inherited behavior. If you find
yourself doing a lot of work in a subclass, you should check whether
the base class provides a simpler way to do it, perhaps with a
delegate. Maybe, maybe not.
To respond to special keyboard events in an NSTableView, I created a
subclass and provided an implementation for keyDown.
To draw some customized stuff in an NSColorWell, I created a subclass
and provided an implementation for drawRect.
Overriding keyDown: and drawRect: (the colons are significant) is not
uncommon.
But I'm told I should be using delegate methods and firstResponders
somehow.
If a delegate method exists that does what you want to do, then it
only makes sense to use it.
In general, is there a preferred approach to using actions over
delegates, and delegates over handling Cocoa events? It seems there
are
many different ways to handle the same problem and some ways are
preferred over others...
You forgot notifications. :)
I'm sorry I don't have time to dig up links -- this topic has been
addressed very well by others.
Bottom line, there isn't a blanket rule or philosophy in Cocoa that
delegates are good and subclassing is bad. Hope this helps.
--Andy
_______________________________________________
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