[英]Change the image of a UIButton when a different Button is clicked
在您潜水之前...默认情况下,ControlState设置为“正常”,并带有图像集
当用户单击按钮时,它会更改按钮的图像,因为我将按钮的目标设置为与所需方法相对应。 当调用-(void)RADIOBUTTONAClicked:时,我需要更改所有其他RADIOBUTTONS的图像
-(void)RadioButtonACLicked:{
//code to change radioButton A, the code i have works
// Code to change all other buttons does not work.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{.... //配置单元格...
....
//-------------Creation of Custom Buttons-------------//
//----RadioButtonA----//
....
[radioButtonA addTarget:self action:@selector(radioButtonAClicked:)forControlEvents:UIControlEventTouchUpInside];
[radioButtonA setImage:img2 forState:UIControlStateNormal];
ButtonB创建
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonB addTarget:self action:@selector(radioButtonBClicked:)forControlEvents:UIControlEventTouchUpInside];
[radioButtonB setImage:img2 forState:UIControlStateNormal];
所以当单击单选按钮时,我需要将所有其他按钮的图像设置为相反。
花了我一段时间使它工作
(void)radioButtonClicked:(UIButton *)sender
{
...
// Sets the UIImage for the custom RadioButtons.
//img refers to radioOn and img2 refers to radioOff.
// uses an if esle conditional block inorder to correctly change the Images if teh conditions are met.
for(int i=0;i<[self.radioButtons count];i++){
if ([[self.radioButtons objectAtIndex:i]tag] == sender.tag) {
[[self.radioButtons objectAtIndex:i] setImage:img2 forState:UIControlStateNormal];
}
[sender setImage:img forState:UIControlStateNormal];
}
请执行下列操作
//When creating the button, set their tags
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];
//Add this line
radioButtonA.tag = 111;
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
//Add this line
radioButtonB.tag = 112;
现在在
-(void)RadioButtonACLicked:{
//code to change radioButton A, the code i have works
//Code to change all other buttons does not work.
radioButtonA = [self.view viewWithTag:111];
radioButtonB = [self.view viewWithTag:112];
//Change the images for radioButton A and B as you wish
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.