如何在 Objective-C 中为 IBInspectable 设置默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27043981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:30:29  来源:igfitidea点击:

How to set default values for IBInspectable in Objective-C?

objective-cxcodeswiftinterface-builderibinspectable

提问by Can Poyrazo?lu

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

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

@IBInspectable var propertyName:propertyType = defaultValuein 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 var propertyName:propertyType = defaultValue在斯威夫特。但是如何在 Objective-C 中实现类似的效果,以便我可以将某些属性的默认值设置为 Interface Builder 中的某些内容?

回答by rintaro

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

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

@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

回答by GetToSet

I wrote my code like this. It works pretty well for me, both when designing in the interface builder or running as a app.

我是这样写我的代码的。无论是在界面构建器中进行设计还是作为应用程序运行时,它对我来说都非常有效。

@interface MyView : UIView

@property (copy, nonatomic) IBInspectable propertyType *propertyName;

@end

- (void)makeDefaultValues {
    _propertyName = defaultValue;
    //Other properties...
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self makeDefaultValues];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self makeDefaultValues];
    }
    return self;
}

回答by Wanbok Choi

I'm using like that

我是这样用的

@IBInspectable var propertyNameValue:propertyType?
var propertyName:propertyType { return propertyNameValue ?? defaultValue }

if propertyNameValuehas nil, propertyNamewill return defaultValue.

如果propertyNameValuenilpropertyName将返回defaultValue

回答by yonel

Why don't use use the macro such as:

为什么不使用使用宏,例如:

#if TARGET_INTERFACE_BUILDER   
// .....IB only specific code when being rendered in IB 
#endif 

???

???

The prepareForInterfaceBuilderselector may also help you to implement IB specific code.

prepareForInterfaceBuilder选择还可以帮助你实现IB特定代码。

More info about those 2 points here:
https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/chapters/CreatingaLiveViewofaCustomObject.html

关于这 2 点的更多信息:https:
//developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/chapters/CreatingaLiveViewofaCustomObject.html

回答by kaspartus

Firstly I tried to override getter and did something like this:

首先,我尝试覆盖 getter 并执行以下操作:

- (UIColor *)borderColor {
    return _borderColor ?: [UIColor greenColor];
}

But in that case I received issue about undeclared identifier _borderColor.

但在那种情况下,我收到了有关未声明标识符的问题_borderColor

Ok, I tried to avoid this issue via custom getter.

好的,我试图通过自定义 getter 来避免这个问题。

- (UIColor *)getBorderColor {
    return _borderColor ?: [UIColor greenColor];
}

Actually it's not a proper getter, as we don't point this method as getter. In case we point we'll receive issue about undeclared identifier, that's why we won't.

实际上它不是一个合适的 getter,因为我们没有将这个方法作为 getter。如果我们指出我们会收到有关未声明标识符的问题,这就是我们不会的原因。

Then we use this method for getting property value in updateUI method.

然后我们使用这个方法在 updateUI 方法中获取属性值。

Also we have to override setter:

我们还必须覆盖 setter:

- (void)setBorderColor:(UIColor *)borderColor {
  _borderColor = borderColor;
  [self updateUI];
}