tags:

views:

33

answers:

1

I have a data-driven method which creates and returns instances of various UIView subclasses. Depending on the input data, it currently returns UIButtons, UITextFields, UILabels, and my own subclass, with more subclasses likely to be added later.

I want to be able to add behavior to whatever I am returning. At present, this behavior consists of drawing a black border, but more, and possibly much more, is likely to become desirable later, as the requirements for my project become fleshed out. The added behavior will depend on the input data -- so at present, depending on the data, the border may or may not be drawn. I want to add this behavior once, in a single place, rather than have to do it separately in each subclass.

This is core functionality of what is likely to become a large project, so design cleanliness and performance are both important considerations.

What approach makes the most sense?

A: 

I would consider using a category. Make the apple default behavior standard, and make the data driven behavior be set by a property. You can then generate all of your components using standard xibs and call your category specialized initializer for each component in there. You could even iterate through your subviews in viewDidLoad to make it happen automatically.

Peter DeWeese
Unless I'm missing something, this would require me to override UIView's drawRect in my category? But Apple strongly discourages this, for what appear to be [good reasons](http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html).
William Jockusch