UIButton in IOS responds to first touch and then does not respond to any subsequent touch events
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];
}
You are creating a new playerOneButton
in the action method without assigning a target/action to it.
精彩评论