简体   繁体   English

UIButton背景图片从另一个按钮更改

[英]UIButton background image change from another button

Is it possible that I can change the background image of some button when I click on another button? 单击另一个按钮时,是否可以更改某个按钮的背景图像?

I basically have 10 buttons, and I want the user to know which button is currently clicked. 我基本上有10个按钮,我希望用户知道当前单击了哪个按钮。 So what happens is each button has 2 images. 因此,每个按钮有2张图像。 One for selected and one for not selected. 一种用于选择,一种用于未选择。

I want to create a function which will reset the background images of all the buttons. 我想创建一个函数,该函数将重置所有按钮的背景图像。 And in the method for each button, I will add this reset function and then change the background image of that specific button. 在每个按钮的方法中,我将添加此重置功能,然后更改该特定按钮的背景图像。

Can someone suggest how is that possible? 有人可以建议这怎么可能吗?

I know how to change the background image of a button when that button is clicked. 我知道单击按钮时如何更改按钮的背景图像。

This how my buttons look: 这是我的按钮外观:

- (IBAction)posterButton:(id)sender {}
- (IBAction)colorInvertButton:(id)sender {}

etc.. 等等..

Look up the documentation for UIButton: configure each button like this: 查找UIButton的文档:像这样配置每个按钮:

[button setControlImage:selectedImage forState:UIControlStateSelected];
[button setControlImage:unselectedImage forState:UIControlStateNormal];

^^this can also be done in interface builder btw. ^^这也可以在界面生成器btw中完成。

There are also the states UIControlStateNormal | UIControlStateHighlighted 还有状态UIControlStateNormal | UIControlStateHighlighted UIControlStateNormal | UIControlStateHighlighted and UIControlStateSelected | UIControlStateHighlighted UIControlStateNormal | UIControlStateHighlightedUIControlStateSelected | UIControlStateHighlighted UIControlStateSelected | UIControlStateHighlighted , but this is optional. UIControlStateSelected | UIControlStateHighlighted ,但这是可选的。

A button also has a selected state, inherited from UIControl . 按钮还具有从UIControl继承的选定状态。

If you want to have only one selected button at a time, try this (_selectedButton should be declared as an instance variable): 如果您一次只希望选择一个按钮,请尝试以下操作(_selectedButton应该声明为实例变量):

- (UIButton *)selectedButton {
    return _selectedButton;
}
- (void)setSelectedButton:(UIButton *)b {
    if(b != _selectedButton) {
        _selectedButton.selected = NO;
        b.selected = YES;
        _selectedButton = b;
    }
}

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

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