[英]UIButton background image change from another button
单击另一个按钮时,是否可以更改某个按钮的背景图像?
我基本上有10个按钮,我希望用户知道当前单击了哪个按钮。 因此,每个按钮有2张图像。 一种用于选择,一种用于未选择。
我想创建一个函数,该函数将重置所有按钮的背景图像。 在每个按钮的方法中,我将添加此重置功能,然后更改该特定按钮的背景图像。
有人可以建议这怎么可能吗?
我知道单击按钮时如何更改按钮的背景图像。
这是我的按钮外观:
- (IBAction)posterButton:(id)sender {}
- (IBAction)colorInvertButton:(id)sender {}
等等..
查找UIButton的文档:像这样配置每个按钮:
[button setControlImage:selectedImage forState:UIControlStateSelected];
[button setControlImage:unselectedImage forState:UIControlStateNormal];
^^这也可以在界面生成器btw中完成。
还有状态UIControlStateNormal | UIControlStateHighlighted
UIControlStateNormal | UIControlStateHighlighted
和UIControlStateSelected | UIControlStateHighlighted
UIControlStateSelected | UIControlStateHighlighted
,但这是可选的。
按钮还具有从UIControl
继承的选定状态。
如果您一次只希望选择一个按钮,请尝试以下操作(_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.