Whirlwind
Whirlwind

Reputation: 13675

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

Upvotes: 6

Views: 1321

Answers (1)

matt
matt

Reputation: 535989

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.

Upvotes: 7

Related Questions