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

  • Home
  • SEARCH
  • 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 6658797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:57:20+00:00 2026-05-26T01:57:20+00:00

I have created a UIButton programmatically that correctly changes background color as desired when

  • 0

I have created a UIButton programmatically that correctly changes background color as desired when button is touched down for the first time. After that, the button does not respond to any subsequent touch events.

I have instrumented the color change method with NSLog and this method is not called after the first touch, even though the button visually appears to respond to subsequent touches on the iPhone screen. It seems the subsequent touches are not actually triggering any event (eg. TouchDown), but I can’t figure out why not.

I have searched every post on sof related to this issue, but none of the answers seem to solve my problem.

The button is declared in the header file as:

UIButton *playerOneButton;
UIButton *playerTwoButton;

Here is the button initialization code that I use after the UIViewController loads:

- (void)viewDidLoad
{
    [super viewDidLoad];

//Create initial view of Button One before any selection is made

playerOneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f);

[playerOneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]];
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];

[self.view addSubview:playerOneButton];

//Create initial view of Button Two before any selection is made

playerTwoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f);

[playerTwoButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];

[self.view addSubview:playerTwoButton];

// Add correct names to both buttons

[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];

// Link buttons to methods that will toggle colors between Green for selected button and Red for unselected button

[playerOneButton addTarget:self action:@selector(setPlayerOneButtonStatus) forControlEvents:UIControlEventTouchDown];

[playerTwoButton addTarget:self action:@selector(setPlayerTwoButtonStatus) 
          forControlEvents:UIControlEventTouchDown];

}

and the method that is invoked to actually change the colors:

-(void) setPlayerOneButtonStatus;
{
playerOneButton.selected = YES;

// Load colored images
UIImage *imageGreen = [UIImage imageNamed:@"button_green.png"];
UIImage *imageRed = [UIImage imageNamed:@"button_red.png"];

// And create the stretchy versions.
float wGreen = imageGreen.size.width / 2, hGreen = imageGreen.size.height / 2;
UIImage *stretchGreen = [imageGreen stretchableImageWithLeftCapWidth:wGreen topCapHeight:hGreen];

float wRed = imageRed.size.width / 2, hRed = imageRed.size.height / 2;
UIImage *stretchRed = [imageRed stretchableImageWithLeftCapWidth:wRed topCapHeight:hRed];

// Now create button One with Green background color
playerOneButton = [UIButton buttonWithType:UIButtonTypeCustom];
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f);

[playerOneButton setBackgroundImage:stretchGreen forState:UIControlStateNormal];

[playerOneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]];
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];

[self.view addSubview:playerOneButton];

 // Now create button Two with Red background color

playerTwoButton = [UIButton buttonWithType:UIButtonTypeCustom];
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f);

[playerTwoButton setBackgroundImage:stretchRed forState:UIControlStateNormal]; 
[playerTwoButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]];

[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];

[self.view addSubview:playerTwoButton];

}
  • 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-26T01:57:20+00:00Added an answer on May 26, 2026 at 1:57 am

    You are creating a new playerOneButton in the action method without assigning a target/action to it.

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

Sidebar

Related Questions

I have a UIButton that is created inside of each table cell. I want
Given that you have a method -(void)aSelector:(id)anyArgument; And have set up a UIButton programmatically
I have created 10 buttons programmatically and set the titles in the button. Now
I have created a custom button called ModuleUIButton which inherits from UIButton . This
I have a button that's created via a NIB file. I've derived a class
I have created buttons are programmatically and set the button tags. View Did Load:
I have successfully created a UITableView/UITableViewCell that is able to slide up and down
I have created a button programmatically and I want to fire event to go
I created a button programmatically: UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.frame = CGRectMake(290, 120,
I have created a custom UIButton (UIButton subclass) for my application. Within this button's

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.