gpt4 book ai didi

ios - 使用 UIAppearance 代理代替普通的默认值有什么好处?

转载 作者:行者123 更新时间:2023-11-28 19:37:48 27 4
gpt4 key购买 nike

请你解释一下,使用 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];

最佳答案

这实际上不是 UIAppearance 的用例。如果您正在编写一个仅在单个项目中使用的 UIView 子类,那么只需使其看起来像您想要的那样。

UIAppearance 用于如果您决定使该子类在您的所有项目中都可用,并且可能需要为每个项目专门设置样式。或者,如果您想使用它的大量实例而不必单独设置每个实例的样式。

类似于 UILabel 的工作方式。它是一个 UIView 子类,旨在用于各种不同的项目。

关于ios - 使用 UIAppearance 代理代替普通的默认值有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37160413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com