0
UIButton
提供許多與狀態相關的設置(圖像,標題顏色等)。 我已經手動添加了一個子視圖到一個按鈕,它將對按鈕狀態的變化做出反應。 我該怎麼做?我應該嘗試在狀態更改上映射UIControlEvent
?用於跟蹤UIButton狀態變化的解決方案
UIButton
提供許多與狀態相關的設置(圖像,標題顏色等)。 我已經手動添加了一個子視圖到一個按鈕,它將對按鈕狀態的變化做出反應。 我該怎麼做?我應該嘗試在狀態更改上映射UIControlEvent
?用於跟蹤UIButton狀態變化的解決方案
您可以通過增加國際志願者組織觀察員按鈕的選擇和突出性能做到這一點,但是這是一個很多比創造UIButton
一個子類和重載的setSelected
和setHighlighted
方法更加複雜。你會是這樣做的:
//MyCustomButton.h
@interface MyCustomButton : UIButton
@end
//MyCustomButton.m
@implementation MyCustomButton
- (void)setUp
{
//add my subviews here
}
- (id)initWithFrame:(CGRect)frame
{
//this is called when you create your button in code
if ((self = [super initWithFrame:frame]))
{
[self setUp];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
//this is called when you create your button in interface builder
if ((self = [super initWithCoder:aDecoder]))
{
[self setUp];
}
return self;
}
- (void)setSelected:(BOOL)selected
{
super.selected = selected;
//update my subviews here
}
- (void)setHighlighted:(BOOL)highlighted
{
super.highlighted = highlighted;
//update my subviews here
}
@end
然後,您可以在代碼在Interface Builder通過拖動定期UIButton
到您的視圖中創建自定義按鈕,或他們,然後設置其類MyCustomButton
的檢查。