Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:54:38+00:00 2026-05-23T08:54:38+00:00

I am creating a subclass of UIButton in order to create my own customized

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:54:38+00:00Added an answer on May 23, 2026 at 8:54 am

    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

    • The UIView Programming Guide: View and Window Architecture -> Tips for Using Views Effectively -> Do Not Customize Controls by Embedding Subviews

    Source: my post here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created my own view by creating a subclass of the SurfaceView class. However
When creating own Activity subclass, we are overriding some of the basic Activity lifecycle
So I'm creating a new file, a subclass of a UIViewController via the Xcode
I have a problem with creating my own subclass of NSData , which I
I am creating a subclass of QAbstractItemModel to be displayed in an QTreeView .
I'm creating a custom subclass of NSPopUpButton and NSPopUpButtonCell to display a lone icon
Creating a view with following code. - (void)loadView { paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
I am creating a subclass of UIBarButtonItem, to provide some specific functionality. The new
I'm creating a new subclass of UITableViewController, and with it the below default implementation.
I've tried creating a UIButton with UIButtonTypeCustom. Then afterwards used the following methods to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.