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

标签 objective-c properties uicolor

为什么我的颜色属性总是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/

相关文章:

ios - 用于视网膜显示的带有 UIImage 的 UIColor 模式

iphone - UIScrollView添加了 subview 但无法滑动到它们

ios - 如何分配 skpayment 应用程序用户名

ios - 如何从 UIColor 获取 RGB 值?

ios - 保留属性(property)被重新分配

java - 使用 JSTL 出现此错误 : javax. el.PropertyNotFoundException

ios - 70% 灰色是什么意思,我如何在 UIColor 中指定它?

ios - bloc.io : failing XTCAssertTest for unknown reasons; previously successful in mock app

c++ - 如何在 IOS 应用程序 (OpenGL) 中放置 href 链接

c# - TargetInvocationException @ PropertyInfo.GetValue(target, null) 与目标类型 MemoryStream