ios - 单击不同的按钮时更改 UIButton 的图像

标签 ios uitableview uibutton

在你开始之前... ControlState 默认设置为 Normal 并设置了 Image

当用户单击按钮时,它会更改按钮的图像,因为我将该按钮的目标设置为与所需的方法相对应。当调用 -(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];

}

关于ios - 单击不同的按钮时更改 UIButton 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10927264/

相关文章:

ios - 找不到 Cordova iOS 插件

android - iOS和Android安装(直接测试)

ios - UIButton 在单元格重新加载时创建两次

ios - 如何在 UITableView 滚动时保持 UITableViewCell 的状态?

jquery 按钮集()

ios - 原生 zlib inflate/deflate for swift3 on iOS

ios - UINavigationBar 中的项目未填充屏幕宽度

ios - UITableViewController 中未调用 cellForRowAtIndex?

ios - `UICollectionView`什么时候结束

iphone - UIButton 方法在 Iphone 上调用但在 Ipad 上不调用