37

I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.

What are the negatives of subclassing a UIButton? And I know it's vague, but what are other alternatives to subclassing a UIButton?

justin
  • 104,054
  • 14
  • 179
  • 226
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 1
    What's your reason to subclass an UIButton? Specify button type `custom`. What else is needed? – ott-- Nov 02 '12 at 20:07
  • I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing. – SirRupertIII Nov 02 '12 at 20:12
  • 4
    There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof. – Feloneous Cat Dec 12 '12 at 19:55
  • Subclassing `UIButton` is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) `UIControl` subclass is always better idea. – Jakub Truhlář Oct 06 '16 at 20:05

3 Answers3

37

The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.

In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.

One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.

edit, your own factory method

I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Category so it's available directly on UIButton.

@interface UIButton ( MyCompanyFactory )
+(UIButton *) buttonWithMyCompanyStyles;
@end
@implementation UIButton
+(UIButton *) buttonWithMyCompanyStyles {
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // [theButton set...
    return theButton;
}
@end
Community
  • 1
  • 1
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • although important to design, i'm not sure "favor composition" is really a good answer to the question because `UIButton` is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted). – justin Nov 02 '12 at 20:41
  • @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere? – Chris Trahey Nov 02 '12 at 21:21
  • 3
    I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile. – justin Nov 02 '12 at 21:27
  • How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything? – Jakub Turcovsky Aug 21 '14 at 17:51
  • @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear – Chris Trahey Aug 21 '14 at 19:56
17

It's because UIButton is kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame: (and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustom as your starting point for a UIButton subclass.


On implementing a subclass of UIButton

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
justin
  • 104,054
  • 14
  • 179
  • 226
13

If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:

[mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];

UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.