选择一个按钮时如何更改多个按钮的按钮图像?
Posted
技术标签:
【中文标题】选择一个按钮时如何更改多个按钮的按钮图像?【英文标题】:How to change the button image of multiple buttons when a button is selected? 【发布时间】:2013-10-29 06:26:21 【问题描述】:我已经动态创建了几个月的 12 个按钮,并为按钮设置了不同的图像。所有按钮都有标签。我使用 switch 方法访问带有标签的每个按钮。选择时我更改了按钮的图像。 But i can't changed the image of other buttons as unselected when a button is selected.
这是我的代码。
//Button Creation in scroll view
NSMutableArray* buttonArray = [[NSMutableArray alloc] init];
for(int i = 0; i < 12; i++)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,5,37,37)];
[btn setTag:i];
[btn setImage:[UIImage imageNamed:[buttonImageArray objectAtIndex:i]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(monthBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[buttonArray addObject:btn];
[btn release];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.scrollEnabled = YES;
menuScrollView.bounces = FALSE;
menuScrollView.delegate = self;
menuButtons = buttonArray;
float totalButtonWidth = 15;
for(int i = 0; i < [menuButtons count]; i++)
UIButton *btn1 = [menuButtons objectAtIndex:i];
CGRect btnRect = btn1.frame;
btnRect.origin.x = totalButtonWidth;
[btn1 setFrame:btnRect];
btn1.userInteractionEnabled=YES;
[menuScrollView addSubview:btn1];
totalButtonWidth += btn1.frame.size.width+25;
[menuScrollView setContentSize:CGSizeMake(totalButtonWidth, 45)];
[self.view addSubview:menuScrollView];
//Button Action Method
-(IBAction)monthBtnAction:(id)sender
NSLog(@"Button Tag is : %i",[sender tag]);
switch ([sender tag])
case 0:
NSLog(@"Tag:%d January",[sender tag]);
flag0 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 1:
NSLog(@"Tag:%d Febraury",[sender tag]);
flag1 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 2:
NSLog(@"Tag:%d March",[sender tag]);
flag2 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 3:
NSLog(@"Tag:%d April",[sender tag]);
flag3 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 4:
NSLog(@"Tag:%d May",[sender tag]);
flag4 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 5:
NSLog(@"Tag:%d June",[sender tag]);
flag5 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 6:
NSLog(@"Tag:%d July",[sender tag]);
flag6 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 7:
NSLog(@"Tag:%d August",[sender tag]);
flag7 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 8:
NSLog(@"Tag:%d September",[sender tag]);
flag8 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 9:
NSLog(@"Tag:%d October",[sender tag]);
flag9 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 10:
NSLog(@"Tag:%d November",[sender tag]);
flag10 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
case 11:
NSLog(@"Tag:%d December",[sender tag]);
flag11 = YES;
theButton = (UIButton *)sender;
[theButton setImage:[UIImage imageNamed:[buttonSelectArray objectAtIndex:[sender tag]]] forState:UIControlStateNormal];
dateValue = [sender tag]+1;
NSLog(@"dateValue:%d",dateValue);
[self viewWillAppear:NO];
break;
【问题讨论】:
【参考方案1】:获取一个 NSButton 类型的类变量,并在 IBAction 方法结束时(在 switch-case 之后),将其值设置为 sender。这将保存最后点击按钮的标签。
现在在IBAction方法的开始(切换前),设置你保存的按钮的图像为取消选择图像。
**.h file**
UIButton* lastClickedButton;
// Your IBAction
-(IBAction)monthBtnAction:(id)sender
[lastClickedButton setImage:[UIImage imageNamed:[buttonUnselectArray objectAtIndex:[lastClickedButton tag]]] forState:UIControlStateNormal];
//Your switch statement
switch
----
----
lastClickedButton = sender;
【讨论】:
它对我有用。您的答案中的一项更正。在 .h 文件中,它不是 NSButton 而是它的 UIButton。请更正。 哦,是的。谢谢。我在 OSX 上工作,所以错过了 :)。现已更正。 嗯,这个答案对我也有帮助。谢谢你:)以上是关于选择一个按钮时如何更改多个按钮的按钮图像?的主要内容,如果未能解决你的问题,请参考以下文章
UITableView 中的 UIButton,选择器应用于多个按钮