的UIButton在IOS响应第一触摸,然后不给
问题描述:
我已经创建了一个UIButton编程按钮时,触摸向下首次为希望正确地更改背景颜色的任何后续触摸事件作出响应。之后,该按钮不会响应任何后续的触摸事件。的UIButton在IOS响应第一触摸,然后不给
我已经仪表与NSLog的颜色变化的方法,该方法在第一触摸后不叫,即使按钮在视觉上显得后续触摸响应在iPhone屏幕上。看起来接下来的触摸实际上并没有触发任何事件(例如TouchDown),但我不明白为什么不能。
我搜索过SOF相关的每一个岗位到这个问题,但没有一个答案似乎解决我的问题。
按钮在头文件中声明:
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
在操作方法中没有为其分配目标/操作。
这完全解决了我的问题,我很感激! – Peter
我希望通过将目标/行动到新创建的按钮,你没有“解决”你的问题。您必须修改现有实例,而不是在每次点击该按钮时都创建一个新实例。 –
感谢您的澄清。我确实按照你的建议解决了这个问题,现在所有人都在完美地工作。 – Peter