3 buttons, a lot of implementations, in a huge project. How do stylish objective-c programmers solve this problem?
This is more a question about design pattern, but I think it is relevant, and I'm struggling to find an answer.
To simplify the problem, let's say I have 3 kinds of buttons in my application, which I'm gonna use a lot. There is a VeryImportantButton
, an ImportantButton
and NotVeryImportantButton
, with different styles: border, corner, colors, etc.
I don't wan't to copy paste everywhere
UIButton *mybutton = [[UIButton alloc] initWithFrame:frame];
[myButton setBackGroundColor:[UIColor redColor];
[myButton.layer setBorderWidth:2.0];
[…]
I can see three solutions:
1/ Subclassing. The 3 buttons would be subclasses of UIButton, the parameters would be set in the implementation file. That way I would just have to alloc-init the desired button.
ImportantButton *mybutton = [[ImportantButton alloc] initWithFrame:frame];
2/ Some helper class (public class methods, a singleton, etc)
UIButton *mybutton = [[UIButton alloc] initWithFrame:frame];
[ButtonImportantnessManager applyVeryImportantStyleToButton:myButton];
3/ Create a category on UIButton
#import "UIButton+Importantness.h"
UIButton *mybutton = [[UIButton alloc] initWithFrame:frame];
[myButton applyVeryImportantStyle];
I can not figure if one of those solutions is better than the other, and which one.
The goal is to have a skinnable application, so IB is just used to lay out the UI elements, not to style them.
I hope this a valid question for SO. Thank you for your help.