简体   繁体   English

当我按下iPhone上的其他按钮时,更改按钮上的图像

[英]Change image on button when I pressed other button on iPhone

In IB I create 9 buttons (custom) and a select for State config: selected (button1.png) disabled (button1_disabled.png).....(button9_disabled.png) 在IB中,我创建9个按钮(自定义)并为状态配置创建一个选择:选定(button1.png)禁用(button1_disabled.png).....(button9_disabled.png)

this tip if only when select button and unselected button 如果仅当选择按钮和未选择的按钮时,此技巧

- (IBAction)onButtonsTapped:(UIButton*)sender {
    [[sender setSelected:![sender isSelected]];
    [priviousSelectedBtn setSelected:NO];
}

but i want to release that: 但我想发布:

When I press button 1 (action) change the picture on the button 1 (setSelected: YES) 当我按下按钮1(动作)时,更改按钮1上的图片(setSelected:是)

I press the button 2 (action) change the picture on the button 2 (setSelected: YES) and change the image on the number 1 (setSelected: NO) 我按下按钮2(动作),更改按钮2上的图片(setSelected:是),并更改数字1(setSelected:NO)上的图像

I press the button 9 (action) change the image on the button 9 (setSelected: YES) and change the image on the button 2 (setSelected: NO) 我按下按钮9(动作),更改按钮9上的图像(setSelected:是),并更改按钮2上的图像(setSelected:NO)

You can create nine outlets for each button and loop through them to check and unselect. 您可以为每个按钮创建九个插座,并在它们之间循环以进行检查和取消选择。 (Codes not compiler checked, you may get some idea) (未经编译器检查的代码,您可能会有所了解)

Or, you can store an outlet for previousSelectedButton. 或者,您可以为previousSelectedButton存储一个插座。

@property(strong)id previousSelectedButton;

- (IBAction)onButtonsTapped:(UIButton*)sender {
    [sender setSelected];
    [previousSelectedButton setSelected:NO];

    if(previousSelectedButton==nil || previousSelectedButton!=sender){
         previousSelectedButton=sender;
    }

}

Try the below code 试试下面的代码

for (int tag = 1; tag <= 9; tag++)//set your tag value as you wish max is 9 as you have mentioned there are 9 buttons
{
    UIButton *btn = (UIButton*)[self.view viewWithTag:tag];

    if (btn.tag != sender.tag)
    {
        [btn setSelected:NO];
    }

}

Hope this may help you. 希望这对您有帮助。 And don't reuse those tag for the superview of those button. 并且不要将这些标记用于这些按钮的监视。 And make a unique series of tag value for each button. 并为每个按钮制作一系列独特的标签值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM