【发布时间】:2016-05-11 10:50:12
【问题描述】:
请您解释一下,使用UIAppearance 代理对象来自定义我的UIView 子类属性而不是创建setter 方法来提供所需的默认值有什么好处,如下面的代码示例:
@interface CustomView : UIView
+ (void)setDefaultBackgroundColor: (UIColor *)defaultBackgroundColor;
@end
@implementation CustomView
static UIColor *defaultBackgroundColor_ = nil;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
if (!defaultBackgroundColor_)
defaultBackgroundColor_ = [UIColor blackColor];
}
return self;
}
+ (void)setDefaultBackgroundColor: (UIColor *)defaultBackgroundColor {
defaultBackgroundColor_ = defaultBackgroundColor;
}
@end
在我读过的所有关于UIAppearance 用法的文章中,他们说,更改此代理的属性允许类的实例自动获得相同的值。我不能完全掌握的是代理对象是否真的需要这个目的?在这种情况下使用静态变量来保存默认值有什么缺点吗?
为了让事情更清楚,我正在比较这种方法:
[CustomView appearance]setBackgroundColor:someNewColor];
用这个:
[CustomView setDefaultBackgroundColor:someNewColor];
【问题讨论】:
标签: ios objective-c uiappearance