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

前端 未结 5 917
面向向阳花
面向向阳花 2021-01-30 16:15

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

@IBInspectable var propertyName:propertyType = defaultValue in Swift. But how do I achieve

相关标签:
5条回答
  • 2021-01-30 16:23

    Why don't use use the macro such as:

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

    ???

    The prepareForInterfaceBuilder selector may also help you to implement IB specific code.

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

    0 讨论(0)
  • 2021-01-30 16:25

    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;
    }
    
    0 讨论(0)
  • 2021-01-30 16:28

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

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

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

    Ok, I tried to avoid this issue via custom 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.

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

    Also we have to override setter:

    - (void)setBorderColor:(UIColor *)borderColor {
      _borderColor = borderColor;
      [self updateUI];
    }
    
    0 讨论(0)
  • 2021-01-30 16:29

    I'm using like that

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

    if propertyNameValue has nil, propertyName will return defaultValue.

    0 讨论(0)
  • 2021-01-30 16:32

    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
    
    0 讨论(0)
提交回复
热议问题