UIAppearance 代理,用于自定义对象
所需做的就是使用 MZ_APPEARANCE_SELECTOR 标记参与外观代理 API 的方法。实现协议方法 + (id)appearance,并在您的 init 或 viewDidLoad 对象方法中调用 applyInvocationTo。
@interface MZViewController : UIViewController <MZApperance>
@property (nonatomic,strong) UIColor *customColor MZ_APPEARANCE_SELECTOR;
@property (nonatomic,assign) CGFloat customFloat MZ_APPEARANCE_SELECTOR;
+ (id)appearance;
@end
+ (id)appearance
{
return [MZAppearance appearanceForClass:[self class]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[[self class] appearance] applyInvocationTo:self];
NSLog(@"custom color: %@",self.customColor);
NSLog(@"custom float: %f",self.customFloat);
}
[[MZViewController appearance] setCustomColor:[UIColor blackColor]];
[[MZViewController appearance] setCustomFloat:6.0];
控制台结果将是:
2013-08-17 19:59:38.546 MZAppearance[3374:c07] custom color: UIDeviceWhiteColorSpace 0 1
2013-08-17 19:59:38.547 MZAppearance[3374:c07] custom float: 6.000000