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

时间:2011-10-04 20:20:23

标签: ios uibutton

我已经以编程方式创建了一个UIButton,可以在第一次触摸按钮时根据需要正确更改背景颜色。之后,该按钮不响应任何后续触摸事件。

我已经使用NSLog检测了颜色更改方法,并且在第一次触摸后不调用此方法,即使按钮在视觉上看起来响应iPhone屏幕上的后续触摸。似乎后续的触摸实际上并没有触发任何事件(例如TouchDown),但我无法弄清楚为什么不这样做。

我搜索过与此问题相关的所有帖子,但没有一个答案似乎解决了我的问题。

该按钮在头文件中声明为:

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

}

1 个答案:

答案 0 :(得分:0)

您正在操作方法中创建新的playerOneButton,而不会为其指定目标/操作。