4

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.

Nayan
  • 3,014
  • 2
  • 17
  • 33
Pierre
  • 390
  • 3
  • 10
  • I've suggested [this method](http://stackoverflow.com/questions/20997851/creating-a-night-theme-for-my-app/21003634#21003634) in another answer. – Travis Jan 17 '14 at 14:20
  • Thank you, this is a great answer. What was the WWDC session you were referring to? So you would suggest the 2nd response, or a 4th with the factory pattern? For example: `VeryImportantButton *myButton = [ButtonImportantnessManager buildVeryImportantButtonWithTitle:(NSString *)title];` ? – Pierre Jan 17 '14 at 15:46
  • The session is from 2012, and it's 216: Advanced Appearance Customization on iOS. I usually do it the first way I suggested, which is to, in my view controller, grab my buttons and theme them myself. I don't usually do the factory way unless most of my buttons are created programatically rather than in interface builder. (By the way, if you like my other answer, please upvote it and the question so that others have an easier time finding it.) – Travis Jan 17 '14 at 16:17

2 Answers2

0

You can globally set attributes for your UIButtons. Like this:

[[UIButton appearance] setBackgroundColor:<#(UIColor *)#>];

So, As you need three buttons of specific kinds, create 3 subClass of UIButton, Set attributes for them globally, as mentioned above & use it frequently in your app.

Let me know if this is not what you want.

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • this will not solve the problem because it will create the property for every button – Retro Jan 17 '14 at 12:11
  • Thank you for your answer, but for `UIButton`, UIAppearance is limited to `contentEdgeInsets`, `setTitleColor:forState:`, `setTitleShadowColor:forState:`and `setBackgroundImage:forState:`. – Pierre Jan 17 '14 at 14:01
  • Yeah..It is what it is. Feel free to accept/upvote if it helped you ..:) @Dustt – Prince Agrawal Jan 17 '14 at 14:09
0

I have seen numerous open source code with custom button and most time its perforable to first method you specified because subclassing is not very perfect for doing UI kinda stuff because its not work perfectly in every scenario. Just create the button and add property you want and add to subview is best way.

Retro
  • 3,985
  • 2
  • 17
  • 41
  • "... its not work perfectly in every scenario" - if not every then which scenario u think? – Nayan Jan 17 '14 at 12:19
  • I mean subclassing the UIButton for UI but for adding methods for layout or frame and functionality is ok! – Retro Jan 17 '14 at 12:22