selected无法重写setter 方法,一次在分类里边强写不赋值,以为只要不引入这个分类其他button不受影响,结果不行。
#import"UIButton+addSelectColor.h"
#import
@implementationUIButton (addSelectColor)
staticchar* keySelectColor ="keySelectedColor";
staticchar*keyNormalColor ="keyNormalColor";
staticchar*keyTitleSelectedColor ="keyTitleSelectedColor";
staticchar*keyTitleNormalColor ="keyTitleNormalColor";
-(void)setSelectedColor:(UIColor*)selectedColor
{
objc_setAssociatedObject(self, &keySelectColor, selectedColor,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor*)selectedColor
{
returnobjc_getAssociatedObject(self, &keySelectColor);
}
-(void)setNormalColor:(UIColor*)normalColor
{
objc_setAssociatedObject(self, &keyNormalColor, normalColor,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor*)normalColor
{
returnobjc_getAssociatedObject(self, &keyNormalColor);
}
-(void)setTitleSelectedColor:(UIColor*)titleSelectedColor
{
objc_setAssociatedObject(self, &keyTitleSelectedColor, titleSelectedColor,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor*)titleSelectedColor
{
returnobjc_getAssociatedObject(self, &keyTitleSelectedColor);
}
-(void)setTitleNormalColor:(UIColor*)titleNormalColor
{
objc_setAssociatedObject(self, &keyTitleNormalColor, titleNormalColor,OBJC_ASSOCIATION_RETAIN);
}
-(UIColor*)titleNormalColor
{
returnobjc_getAssociatedObject(self, &keyTitleNormalColor);
}
-(void)setTabSelected:(BOOL)tabSelected
{
if(tabSelected ==YES) {
self.backgroundColor=self.selectedColor;
[selfsetTitleColor:self.titleSelectedColorforState:UIControlStateNormal];
}else{
self.backgroundColor=self.normalColor;
[selfsetTitleColor:self.titleNormalColorforState:UIControlStateNormal];
}
}
//-(void)setSelected:(BOOL)selected
//{
////self.selected = selected;
//if (selected == YES) {
//self.backgroundColor = self.selectedColor;
//[self setTitleColor:self.titleSelectedColor forState:UIControlStateNormal];
//} else {
//self.backgroundColor = self.normalColor;
//[self setTitleColor:self.titleNormalColor forState:UIControlStateNormal];
//}
//}
//
最后注掉这些,重写了一个方法-(void)setTabSelected:(BOOL)tabSelected来代替实现.
看来一旦在分类里边重写UIKit的方法,所有地方都会被重写,这很危险。