Search code examples
objective-cxcodeuikitibdesignableibinspectable

IBInspectable property doesn't have its value in initWithCoder: initializer


I am having this in MyClass.h file:

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface MyClass : UIView

@end

and this is the code from the .m file:

-(void)awakeFromNib{
    [super awakeFromNib];

    NSLog(@"This works %lud",(unsigned long)self.max);
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{

    if ((self = [super initWithCoder:aDecoder])){

        NSLog(@"This doesn't work %lud",(unsigned long)self.max);
    }

    return self;
}

In awakeFromNib method, I get a correct value set in IB. In initWithCoder: I get this value equal to zero.

What is the appropriate time to check for a value, set in IB, for this property?

This is how I have defined a property in .m file:

@interface MyClass()

@property (nonatomic) IBInspectable NSUInteger max;

@end

Solution

  • You seem to have answered your own question: awakeFromNib instead of initWithCoder:. awakeFromNib is later, and by that time, the runtime attributes have been set.

    In real life, of course, you probably wouldn't care about this value until we get to viewDidLoad, which is considerably later than both.