IB_DESIGNABLE未显示在Interface Builder

时间:2015-09-16 09:08:48

标签: ios objective-c xcode ibdesignable

我正在使用Objective-C在XCode 6.4中制作自定义UI组件。在每次更改IBInspectable值之一时使用组件的主故事板中,我收到以下错误:

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.

我正在加载一个.xib文件而我正在覆盖initWithCoder&amp; initWithFrame方法如下:

- (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;
}

在initWithCoder中:我必须使用检查是否存在子视图,因为它进入了无限循环(loadView中的xib文件再次调用initWithCoder。

因此,当我构建并运行它时,它会正确显示(但只有NIB中的默认值而不是我在故事板中传递的正确值),但组件的实时预览不会显示在Interface Builder中。

更新

这是自定义UI组件的头文件

#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CustomComponent : UIView

@property (assign, nonatomic) IBInspectable UIImage *icon;

@property (weak, nonatomic) IBOutlet UIButton *iconButtton;

@end

0 个答案:

没有答案