【问题标题】:IB Designables Error when using UnitTests使用 UnitTests 时出现 IB Designables 错误
【发布时间】:2015-01-24 12:21:48
【问题描述】:

我目前正在使用 Xcode 6.1.1 和 Objective-C 开发 iOS 应用程序。在应用程序中有几个自定义视图子类。其中一个是 UIButton 的子类,另一个是 UIView 的子类。

这些自定义视图被标记为 IB_DESIGNABLE,并具有多个 IBInspectable 属性。
我还在 Xcode 项目中使用单元测试。每次我打开故事板时,Xcodes 都会给我几个错误。

IB Designables
Failed to update auto layout status: dlopen(UnitTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest 
Referenced from UnitTests.xctest
Reason: image not found

IB Designables 
Failed to render instance of CustomRadioButton: dlopen(UnitTests.xctest, 1): Library not loades: @rpath/XCTest.framework/XCTest 
Referenced from UnitTests.xctest
Reason: image not found

当我删除 IB_DESIGNABLE 语句时,错误就会消失。不幸的是,我需要它们是 IB_DESIGNABLE。 我在 StackOverflow 上找到了一篇有相同问题但使用 Swift 的帖子。建议的解决方案在使用 Objective-C 时不起作用,因此用于解决问题的功能在 Objective-C 中不存在(据我所知)。

这里是问题的链接:IBDesignable Errors When Adding to Tests Target

有人知道如何解决这个问题吗?
我也尝试使用 Xcode 6.2 beta,但问题仍然存在。

作为一个评论请求,这是我的自定义视图之一的代码:

CustomButton.h #导入

@interface CustomButton : UIButton

@property (nonatomic, strong) UIFont *titleFont UI_APPEARANCE_SELECTOR;
@property (nonatomic, assign) IBInspectable BOOL useShapedForm;

@property (nonatomic, strong) IBInspectable UIColor *defaultColor;
@property (nonatomic, strong) IBInspectable UIColor *selectedColor;
@property (nonatomic, strong) IBInspectable UIColor *disabledColor;

@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;

- (void)setDefaults;

- (IBAction)touchDown:(id)sender;
- (IBAction)handleButtonClick:(id)sender;

@end

CustomButton.m

#import "CustomButton.h"

@implementation CustomButton

#pragma mark - Initialisation

- (instancetype)init
{
    if (self = [super init]) {
        [self setDefaults];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setDefaults];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setDefaults];
    }
    return self;
}


- (void)setDefaults
{
    self.useShapedForm = NO;

    self.cornerRadius = self.useShapedForm ? 5 : 0;

    self.defaultColor = self.useShapedForm ? [UIColor rsm_bg_btn_2] : [UIColor clearColor];
    self.selectedColor = self.useShapedForm ? [UIColor rsm_bg_btn_1] : [UIColor clearColor];
    self.disabledColor = self.useShapedForm ? [UIColor rsm_bg_btn_3] : [UIColor clearColor];

    if (!self.useShapedForm) {
        [self setTitleColor:[UIColor rsm_font_333_dark] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor rsm_bg_red] forState:UIControlStateSelected];
        [self setTitleColor:[UIColor rsm_font_333_light] forState:UIControlStateDisabled];
    } else{
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}


#pragma mark - Properties

- (void)setTitleFont:(UIFont *)titleFont
{
    if (self.titleFont != titleFont) {
        self.titleFont = titleFont;
        [self.titleLabel setFont:self.titleFont];
    }
}

- (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets
{
    UIEdgeInsets insets = UIEdgeInsetsMake(titleEdgeInsets.top - 10,
                                           titleEdgeInsets.left,
                                           titleEdgeInsets.bottom,
                                           titleEdgeInsets.right);
    [super setTitleEdgeInsets:insets];
}

- (CGSize)intrinsicContentSize
{
    CGSize defaultMetric = [super intrinsicContentSize];
    // TODO: Metrics anpassen!!
    return CGSizeMake(defaultMetric.width, 39);
}


#pragma mark - Private Methods

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    if (self.useShapedForm) {
        [self drawRSMShape];
    }
}


#pragma mark - Public Methods

- (void)drawRSMShape
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                               byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft
                                                     cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
    [path closePath];

    if (self.selected) {
        [self.selectedColor setFill];
    }
    else if (self.enabled){
        [self.defaultColor setFill];
    }
    else {
        [self.disabledColor setFill];
    }

    [path fill];
}


- (IBAction)touchDown:(id)sender
{
    RSMButton *button = (RSMButton *)sender;
    button.highlighted = NO;
}


- (IBAction)handleButtonClick:(id)sender
{

}

@end

【问题讨论】:

  • 看代码问题就更清楚了。
  • 我将代码添加到问题的自定义视图之一中。

标签: objective-c xcode interface-builder ibdesignable


【解决方案1】:

我也遇到了这个问题。这是由 IB Designable 文件在项目和测试项目中都具有目标成员资格引起的。一旦我从测试项目中删除它,清理并重新启动 Xcode,错误就消失了

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 2015-01-04
    • 1970-01-01
    • 2015-02-07
    • 2018-06-24
    • 2018-07-07
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多