[英]How to change button's Image on clicking it in ios
Confession: I have been through every possible link I could find, but unfortunately none work for me. 告白:我一直在尽我所能找到所有可能的联系,但不幸的是,没有一个对我有用。
I am creating buttons dynamically. 我正在动态创建按钮。 When a button is selected, it should change the color of the selected button and the rest should remain the default color. 选择按钮后,它应更改所选按钮的颜色,其余按钮应保持默认颜色。 This way, the user can identify which button is selected. 这样,用户可以识别选择了哪个按钮。
Suppose there are 3 buttons: all blue in color, and when I select the first one, it should change to white and other two should remain blue. 假设有3个按钮:全部为蓝色,当我选择第一个按钮时,应更改为白色,而另两个应保持蓝色。 When I select the second, the first one should go back to blue and the second should now be white. 当我选择第二个时,第一个应该回到蓝色,而第二个现在应该是白色。
btnCounts data I am fetching from server and would vary. btnCounts我从服务器获取的数据会有所不同。
-(void)createButtons{
for (int i=0; i<btnCounts; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTag:i];
[btn setFrame:CGRectMake(xVal, 0, width/btnCounts, 40)];
// [btn setImage:[UIImage imageNamed:@"btn_image.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"white.png"] forState:UIControlStateSelected];
[btn setTitle:name forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrV addSubview:btn];
xVal += self.view.frame.size.width/btnCounts+1;
}
}
Now, in btnClicked: method I am passing the button as a parameter, so that I can use selected button. 现在,在btnClicked:方法中,我将按钮作为参数传递,以便可以使用选定的按钮。
-(void)btnClicked:(UIButton *)button
{
int tag = (int)button.tag;
//...
}
Please help me to find what I am missing here. 请帮助我在这里找到我所缺少的。
Thanks. 谢谢。
Create an instance UIButton
variable to house the selected button so that you can deselect it when the next button is tapped. 创建一个实例UIButton
变量来容纳选定的按钮,以便在点击下一个按钮时可以取消选择它。 If you only pass the currently-tapped button to your btnClicked:
method, you won't know which button to deselect (if any). 如果仅将当前点击的按钮传递给btnClicked:
方法,则将不知道要取消选择哪个按钮(如果有)。
@implementation ClassName {
UIButton *previousButton;
}
...
- (void)btnClicked:(UIButton *)button
{
if(previousButton)
[previousButton setBackgroundColor:[UIColor blueColor]];
previousButton = button;
[button setBackgroundColor:[UIColor whiteColor]];
// do whatever else you need to do here
}
For that you need to store selected button in temporary object. 为此,您需要将所选按钮存储在临时对象中。
Create button in interface(.h) file: 在interface.h文件中创建按钮:
UIButton *selectedButton;
Than add following line in -(void)createButtons
method before for loop. 在for循环之前,在-(void)createButtons
方法中添加以下行。
selectedButton = nil;
for() {...}
Here selectedButton
is set to nil because initially none of the buttons are selected. 这里的selectedButton
设置为nil,因为最初没有选择任何按钮。
And finally replace your -(void)btnClicked:(UIButton *)button
method with following method. 最后用以下方法替换-(void)btnClicked:(UIButton *)button
方法。
-(void)btnClicked:(UIButton *)button {
if(selectedButton) {
selectedButton.selected = NO;
}
button.selected = YES;
selectedButton = button;
}
If you want to make any button initially selected than assign that button to selectedButton
like in btnClicked method. 如果您要使任何按钮最初被选中,而不是像btnClicked方法那样将该按钮分配给selectedButton
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.