2012-01-25 99 views
0

UIButton提供许多与状态相关的设置(图像,标题颜色等)。 我已经手动添加了一个子视图到一个按钮,它将对按钮状态的变化做出反应。 我该怎么做?我应该尝试在状态更改上映射UIControlEvent用于跟踪UIButton状态变化的解决方案

回答

6

您可以通过增加国际志愿者组织观察员按钮的选择和突出性能做到这一点,但是这是一个很多比创造UIButton一个子类和重载的setSelectedsetHighlighted方法更加复杂。你会是这样做的:

//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的检查。

相关问题