Why shouldn't I subclass a UIButton?

27,284

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
27,284
SirRupertIII
Author by

SirRupertIII

Updated on November 16, 2020

Comments

  • SirRupertIII
    SirRupertIII over 3 years

    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
    justin over 11 years
    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).
  • Chris Trahey
    Chris Trahey over 11 years
    @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?
  • justin
    justin over 11 years
    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.
  • Jakub Turcovsky
    Jakub Turcovsky over 9 years
    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?
  • Chris Trahey
    Chris Trahey over 9 years
    @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
  • Iulian Onofrei
    Iulian Onofrei over 5 years
    @Nate, Fixed it.