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