IOS中的UIButton响应第一次触摸,然后不响应任何后续的触摸事件



我以编程方式创建了一个UIButton,当按钮第一次被触摸时,它会正确地改变背景颜色。在此之后,按钮不再响应任何后续的触摸事件。

我已经用NSLog测试了颜色改变方法,这个方法在第一次触摸之后不会被调用,即使按钮在视觉上似乎对iPhone屏幕上的后续触摸做出了响应。似乎随后的触摸实际上并没有触发任何事件(例如:触地),但我不明白为什么不。

我搜索了所有关于这个问题的帖子,但是没有一个答案能解决我的问题。

按钮在头文件中声明为:

UIButton *playerOneButton;
UIButton *playerTwoButton;

这是按钮初始化代码,我使用后UIViewController加载:

- (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];
}

和实际改变颜色所调用的方法:

-(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];
}

您正在操作方法中创建一个新的playerOneButton,而没有为其分配目标/操作。