By KKendall


2012-11-02 19:55:59 8 Comments

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?

3 comments

@justin 2012-11-02 20:20:15

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 2018-11-15 15:06:04

@Nate, Fixed it.

@James Matthew Mudgett 2015-06-10 19:13:17

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.

@Chris Trahey 2012-11-02 20:01:35

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

@justin 2012-11-02 20:41:39

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 2012-11-02 21:21:16

@justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

@justin 2012-11-02 21:27:16

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 2014-08-21 17:51:56

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 2014-08-21 19:56:37

@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

Related Questions

Sponsored Content

90 Answered Questions

5 Answered Questions

[SOLVED] UIButton remove all target-actions

  • 2010-07-27 04:50:14
  • SK9
  • 67810 View
  • 313 Score
  • 5 Answer
  • Tags:   ios uibutton

24 Answered Questions

[SOLVED] How do you add multi-line text to a UIButton?

40 Answered Questions

[SOLVED] Passing Data between View Controllers

15 Answered Questions

[SOLVED] Set UIButton title UILabel font size programmatically

33 Answered Questions

[SOLVED] How do I create a basic UIButton programmatically?

32 Answered Questions

[SOLVED] Can I embed a custom font in an iPhone application?

  • 2008-12-11 20:21:11
  • Airsource Ltd
  • 233045 View
  • 750 Score
  • 32 Answer
  • Tags:   ios cocoa-touch fonts

10 Answered Questions

[SOLVED] How to set the title of UIButton as left alignment?

  • 2010-05-04 11:56:19
  • Madan Mohan
  • 185585 View
  • 426 Score
  • 10 Answer
  • Tags:   objective-c uibutton

2 Answered Questions

[SOLVED] Subclassing UIButton

1 Answered Questions

[SOLVED] simple subclassing of UIButton in iOS

Sponsored Content