14

I am creating a subclass of UIButton in order to create my own customized buttons. My code as follows:

//interface file (subclass of uIButton
@interface UICustomButton : UIButton 
{
    Answer *answer;
    NSString *btnType;
}

@property (nonatomic, retain) Answer *answer;
@property (nonatomic, assign) NSString *btnType;

- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame; 
- (void)buttonPressed;

@end


//Implementation file (.m)
@implementation UICustomButton
@synthesize answer,btnType;

- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame; 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.backgroundColor = [UIColor colorWithHexString:@"#E2E4E7"];

    }

    [self addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlStateNormal];

    self.answer = ans;
    self.btnType = type;

    return self;
}

I am facing some issues in getting the above code to work. I have 2 problems

1) The buttons are not responding to the selector method "buttonPressed"

2) I am hitting a runtime error for the lines 'self.answer = ans' and 'self.btnType = type' Stack trace as follows:

-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0
2011-06-23 00:55:27.038 onethingaday[97355:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0'

What am I doing wrong here?

Zhen
  • 12,361
  • 38
  • 122
  • 199
  • I found that just calling [super init] in the subclass does not work, at least in iOS8. It just plainly returns nil. So I call [super initWithFrame:CGRectZero], then you have to set some size in code... – Jonny Jul 09 '14 at 07:27
  • @albertamg, You should consider removing your comment! It's 2015 and is WRONG and obsolete. – Iulian Onofrei Sep 14 '15 at 08:18
  • @IulianOnofrei you are right. That comment was 4 years old and it is now obsolete. – albertamg Sep 14 '15 at 08:42

5 Answers5

30

This is happening because you are creating a UIButton type object and not a UICustomButton type inside the init method when you do

self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];

Try replacing your init method for

- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame; 
{
    self = [self initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    if (self) 
    {
        self.backgroundColor = [UIColor colorWithHexString:@"#E2E4E7"];

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

        self.answer = ans;
        self.btnType = type;
    }

    return self;
}

This will cause self to be a UICustomButton type object.

Also, you are using a wrong type for the UIControlState parameter when you add the target to your button using the addTarget:action:forControlEvents: method

You should use value among the ones bellow:

UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel


EDIT: Notes on UIButton subclassing

Many references on the web say you should NOT subclass the UIButton class, but not only anybody said why but what also deeply annoyed me was that the UIButton Class Reference does not say anything about it at all.

If you take UIWebView Class Reference for example, it explicitly states that you should not subclass UIWebView

Subclassing Notes The UIWebView class should not be subclassed.

the big deal with UIButton is that it inherits from UIControl and a good and simple explanation is on the UIControl Class Reference itself

Subclassing Notes You may want to extend a UIControl subclass for either of two reasons:

  • To observe or modify the dispatch of action messages to targets for particular events
  • To provide custom tracking behavior (for example, to change the highlight appearance)

So, this means that you CAN subclass a UIButton, but you should be careful on what you are doing. Just subclass it to change its behavior and not its appearance. To modify a UIButton appearance you should use the interface methods provided for that, such as:

setTitle:forState:
setBackgroundImage:forState:
setImage:forState:

References worth reading

Source: my post here

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
  • 2
    -1. If this works, it is a hack at best. There is only one correct way to make a button, that is with the factory method `+buttonWithType:`. – Eiko Jun 22 '11 at 17:05
  • 1
    If it was not meant to be subclassed, it would be on the docs (just as they explicitly state for UIWebView). And Apple also has several projects where they use `UIButton`'s `initWithFrame:` method to create a button, such as UICatalog, for example: http://developer.apple.com/library/ios/#samplecode/UICatalog/Listings/ButtonsViewController_m.html#//apple_ref/doc/uid/DTS40007710-ButtonsViewController_m-DontLinkElementID_8 . I just think you are overreacting ;) – Felipe Sabino Jun 22 '11 at 17:21
  • 1
    No. The docs clearly only offer one method for creation: `buttonWithType:`. The buttonType property is simply undefined when subclassing, and there is no way to set it. Feel free to use it. But when advocating to do so, you should also mention that even a minor iOS update might break your application. Not saying it's extremely likely to happen, but it might not be as unlikely as you might think. Just consider a single vital property that gets added in a future iOS version, that buttonWithType: sets but you fail to do (as you simply don't know about it). Or a different default buttonType. – Eiko Jun 22 '11 at 21:56
  • 9
    If I follow this way of thinking I would not be able to create a `UIView` as the doc does not show the `init` method as well. You just forgot the inheritance part. There is no need for the `UIButton` doc to show the `init` method because it is inherited from `NSObject`. Take `UIImageView` for example, the doc has a `initWithImage` method but no `init`, even though nothing stops me creating it with just `init` and setting the `image` property afterwards. – Felipe Sabino Jun 23 '11 at 00:05
  • Technically you can. If you think of designated initializers, you might want to reconsider your options. Many rules for Obj-C are easy to break without even a compiler warning - yet they should be considered bugs. Oh wait - there is no documented public initializer for UIButton at all. Let's just guess its behavior. – Eiko Jun 23 '11 at 00:21
  • 1
    I added some considerations on UIButton's subclassing issues based on this discussion. Hopefully it will make my point clear... – Felipe Sabino Jun 28 '11 at 14:39
  • Sorry to say, but this still wrong. You cannot reasonably change the behavior, because you just don't know what the behavior of the base class is! In reality when coding to the docs (using the only given factory method for button creation) you get an arbitrary subclass with some implementation. There is **no guarantee** whatsoever what UIButton will provide as a base class. You just rely on undefined behavior at best. – Eiko Jun 28 '11 at 15:07
  • And I must add: It is ok if your code works for you and you are obviously prepared to take the risk. But when recommending wrong code you take great responsibility. – Eiko Jun 28 '11 at 15:11
  • 1
    So you say that the documentation is wrong? I am really interested in finding why, can you show some apple doc saying you should not do that? I just found docs saying that I can change when subclassing a `UIControl` class and the only thing I found about something I can't do is about adding subviews (also to a `UIControl`) – Felipe Sabino Jun 28 '11 at 15:27
  • 1
    I am not saying that you are wrong, I just want some clarification... You have to agree at least that is very simple to have a belief and say that others are wrong and stupid for not believing in it as well. If you show any apple doc confirming what you state I will be very glad to "fix" my answer :) – Felipe Sabino Jun 28 '11 at 15:30
  • The docs don't tell you not to. They *do* state however that there is only one way to create a button. You cannot use this when subclassing, so you are left with an undefined base object after calling init on super. It might be configured how you like it, it might be not. And you obviously have no way to configure vital properties (i.e. buttonType) but rely on some current undocumented behavior. It might work - it might break, nobody knows. Also you are missing on *any* real implementation hidden in the cluster. I think they shortly mentioned it in one of their WWDC videos as well. – Eiko Jun 28 '11 at 16:26
  • Subclassing UIButton is ok: "If you want to create an instance of a specific subclass, you must alloc/init the button directly." – yuf Oct 03 '13 at 19:23
10

Not sure this was in the docs before, but anyway these are the current notes on + (id)buttonWithType:(UIButtonType)buttonType...

To me it looks like subclassing is OK as long as you use init instead of buttonWithType. I have yet to try it myself however.

Discussion This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.

When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.

Jonny
  • 15,955
  • 18
  • 111
  • 232
1

If you want to get notifications when the user is interacting with your buttons, just sublcass UIButton and implement these methods:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesCancelled");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");
}

No init method required.

kanstraktar
  • 5,357
  • 2
  • 21
  • 29
1

Edit

This answer reaches back several years, and things have changed - as Apple docs now explicitly mention subclassing and gives some hints.

So the following answer might be irrelevant or wrong for current development and might be ignored if you're interested in the current state of the art.


UIButton is not meant to be subclassed.

You are better off making a category and defining a factory method that delivers your needed button (with proper call to buttonWithType:). initWithFrame: is not the correct way to initialize a button anyway.

Eiko
  • 25,601
  • 15
  • 56
  • 71
  • I'd love to know the reason for the down voting. Almost every single resource on the web tells you to not subclass UIButton. – Eiko Jun 23 '11 at 00:41
  • 3
    could you give a decent resource for doing this please? I find the Apple docs quite confusing. – Rob W Jun 25 '12 at 16:59
  • 11
    Actually, the UIButton docs explicitly mention subclassing, at least as of iOS 6.1. See Jonny's answer. – Sea Coast of Tibet May 20 '13 at 14:27
0
//
//  BtnClass.m

#import "BtnClass.h"

@implementation BtnClass

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code


    }
    return self;
}

//added custum properities to button
-(id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"initWithCoder");
    self = [super initWithCoder: aDecoder];
    if (self) {
        // Initialization code

        _numberOfItems=[[UILabel alloc]initWithFrame:CGRectMake(40, 8, 160, 30)];
        _numberOfItems.textAlignment=NSTextAlignmentLeft;
        _numberOfItems.font = [UIFont boldSystemFontOfSize:18.0];
        _numberOfItems.textColor = [UIColor darkGrayColor];
        [self addSubview:_numberOfItems];
        _leftImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
        [self addSubview:_leftImage];
        _rightImage=[[UIImageView alloc]initWithFrame:CGRectMake(280, 10, 15, 15)];
        [self addSubview:_rightImage];
        [self setImage:[UIImage imageNamed:@"list-bg2-1.png"] forState:UIControlStateNormal];
        [_rightImage setImage:[UIImage imageNamed:@"carat.png"]];
        self.backgroundColor=[UIColor blackColor];


        if(self.tag==1)
        {
            [_leftImage setImage:[UIImage imageNamed:@"notes-icon.png"]];

        }
        if(self.tag==2)
        {
            [_leftImage setImage:[UIImage imageNamed:@"photos-icon.png"]];

        }
        if(self.tag==3)
        {
            [_leftImage setImage:[UIImage imageNamed:@"videos-icon.png"]];

        }


    }
    return self;
}

//selected method of uibutton
-(void)setSelected:(BOOL)selected
{

    [super setSelected:selected];


    if(selected)
    {
        [self setImage:nil forState:UIControlStateNormal];
        _numberOfItems.textColor = [UIColor whiteColor];

        [_rightImage setImage:[UIImage imageNamed:@"carat-open.png"]];

        if(self.tag==1)
        {
            [_leftImage setImage:[UIImage imageNamed:@"white-notes-icon.png"]];
        }
        else if(self.tag==2)
        {

            [_leftImage setImage:[UIImage imageNamed:@"white-photo-icon.png"]];

        }
        else
        {
            [_leftImage setImage:[UIImage imageNamed:@"white-video-icon.png"]];

        }

    }
    else{

        _numberOfItems.textColor = [UIColor darkGrayColor];

        if(self.tag==1)
        {
            [_leftImage setImage:[UIImage imageNamed:@"notes-icon.png"]];

        }
        if(self.tag==2)
        {
            [_leftImage setImage:[UIImage imageNamed:@"photos-icon.png"]];

        }
        if(self.tag==3)
        {
            [_leftImage setImage:[UIImage imageNamed:@"videos-icon.png"]];

        }

        [self setImage:[UIImage imageNamed:@"list-bg2-1.png"] forState:UIControlStateNormal];

        [_rightImage setImage:[UIImage imageNamed:@"carat.png"]];

    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
Sishu
  • 1,510
  • 1
  • 21
  • 48