I'm making a custom UI component in XCode 6.4, using Objective-C. In the main storyboard where I use the component whenever I change one of the IBInspectable values I get the following error:
Main.storyboard: warning: IB Designables: Ignoring user defined runtime attribute for key path "icon" on instance of "UIView". Hit an exception when attempting to set its value: [<UIView 0x7fc33a5601c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key icon.
I'm loading a .xib file and I'm overriding the initWithCoder & initWithFrame methods as follows:
- (void) loadView {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
UIView *view = [[bundle loadNibNamed:@"TheNameOfTheNIB" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[iconButtton setImage:_icon forState:UIControlStateNormal|UIControlStateSelected];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self && self.subviews.count == 0) {
[self loadView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self loadView];
}
return self;
}
In initWithCoder: I had to use a check if there was a subview because it was going into an infinite loop (the xib file in loadView is calling initWithCoder: again.
So when I build and run this it shows up correctly (but only with the default values in the NIB not with the correct ones I pass in the storyboard), but the live preview of the component doesn't show up in Interface Builder at all.
Update
Here's the header file for the custom UI component
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CustomComponent : UIView
@property (assign, nonatomic) IBInspectable UIImage *icon;
@property (weak, nonatomic) IBOutlet UIButton *iconButtton;
@end