如何在Objective-C中设置IBInspectable的默认值? [英] How to set default values for IBInspectable in Objective-C?

查看:462
本文介绍了如何在Objective-C中设置IBInspectable的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道IBInspectable-properties的默认值可以设置为:

I know default values of IBInspectable-properties can be set as:

@IBInspectable var propertyName:propertyType = defaultValue 在斯威夫特。但是我如何在Objective-C中实现类似的效果,以便我可以在Interface Builder中将某个属性的默认值设置为某个?

@IBInspectable var propertyName:propertyType = defaultValue in Swift. But how do I achieve a similar effect in Objective-C so that I can have default value of some property set to something in Interface Builder?

推荐答案

由于 IBInspectable 值是在 initWithCoder:之后和之前设置的awakeFromNib:,您可以在 initWithCoder:方法中设置默认值。

Since IBInspectable values are set after initWithCoder: and before awakeFromNib:, you can set the defaults in initWithCoder: method.

@interface MyView : UIView
@property (copy, nonatomic) IBInspectable NSString *myProp;
@property (assign, nonatomic) BOOL createdFromIB;
@end

@implementation MyView

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self != nil) {
        self.myProp = @"foo";
        self.createdFromIB = YES;
    }
    return self;
}

- (void)awakeFromNib {
    if (self.createdFromIB) {
        //add anything required in IB-define way
    }
    NSLog(@"%@", self.myProp);
}

@end

这篇关于如何在Objective-C中设置IBInspectable的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆