gpt4 book ai didi

objective-c - 如何正确使用 UIColor 作为属性

转载 作者:行者123 更新时间:2023-12-04 05:00:54 25 4
gpt4 key购买 nike

为什么我的颜色属性总是null ?

@interface StreamingMediaControlButton : UIButton

@property (strong, nonatomic) UIColor *color;

@end

@implementation StreamingMediaControlButton

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_color = [UIColor whiteColor];
}
return self;
}

- (void)drawRect:(CGRect)rect
{
NSLog(@"color: %@", self.color);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, self.color.CGColor);
}
@end

最佳答案

您之前的评论说您正在使用界面构建器,这意味着在按钮上调用了不同的 init 方法。你需要实现:

- (id)initWithCoder:(NSCoder *)decoder
{
if ((self = [super initWithCoder:decoder])) {
// setup code
}

return self;
}

很多时候最好同时实现 initWithCoder:initWithFrame:并让他们都调用 commonInit共享设置代码所在的方法。

关于objective-c - 如何正确使用 UIColor 作为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16162142/

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