iPhone -- Adding behavior to multiple UIView subclasses
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 proje开发者_高级运维ct, so design cleanliness and performance are both important considerations.
What approach makes the most sense?
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.
精彩评论