1

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
zbrox
  • 2,683
  • 3
  • 25
  • 29
  • Can we see the parts of your code where you add the IB_DESIGNABLE and IBInspectable attributes? – Matt Gibson Sep 16 '15 at 09:32
  • @MattGibson I will do this right away – zbrox Sep 16 '15 at 11:16
  • There might be some help for you at http://nshipster.com/ibinspectable-ibdesignable/ – AMayes Sep 17 '15 at 00:06
  • It only goes over the standard things about IB_DESIGNABLE, nothing much about this specific problem. The way of debugging described there is also not yielding anything. – zbrox Sep 17 '15 at 10:52
  • Can u try to do it in layoutSunbview method. – Mike.R Sep 18 '15 at 14:48
  • @Mike.R I assume you mean to put [self loadView] in viewDidLayoutSubviews. I tried it now and didn't give different results. – zbrox Sep 21 '15 at 12:44
  • @zbrox I mean try to do your changes in layoutSunbview (I think u should not call [self loadView] directly , http://stackoverflow.com/questions/8876212/proper-use-of-loadview-and-viewdidload-with-uiviewcontroller-without-nibs-xibs), also please try to do something simple like change the background colour for the beginning. ("What's New in Interface Builder" WWDC2014 they explain how to do it). – Mike.R Sep 21 '15 at 13:42

0 Answers0