简体   繁体   English

如何在iOS中单击按钮时更改按钮的图像

[英]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.

相关问题 通过单击按钮IOS7更改按钮图像 - Change button image by clicking on the button IOS7 如何在iOS中点击时更改自定义按钮的图像? - How to change Custom Button's Image when Tapping in iOS? 如何通过单击iOS中的按钮更改CustomAnnotation中的UILabel值 - How to change UILabel value in CustomAnnotation by clicking button in iOS 单击按钮时如何从图库中选择图像(Xamarin iOS) - How to select an image from gallery when clicking a button (Xamarin iOS) 如何根据变量在SWIFT中更改图像,单击使用IF语句的按钮 - How to change an image in SWIFT depending on a variable, clicking a button that uses an IF statement Swift:如何在单击提交按钮时更改UIButton图像 - Swift: How to Change UIButton Image on clicking submit button 如何更改iOS按钮上的背景图像? - How to change background image on a button , iOS? 带有图像的按钮,单击按钮iOS 10时图像消失 - Button with Image, Image Disappear when clicking the button iOS 10 有没有更好的方法通过单击按钮来更改 ImageView 中的图像? - Is there a better way to change an Image in an ImageView by clicking a button? iOS-如何在不调整按钮视图大小和位置的情况下更改按钮图像 - iOS - how to change button image without resizing and repositioning the button view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM