1

我有一个 UIView 的子类,我是从一个 NIB 文件加载的。我的 NIB 文件,包含一个 UIButton 和一个 IBOutlet,称为时钟

@property (strong, nonatomic) IBOutlet UIButton *clock;

在我的界面构建器上,我的时钟按钮中设置的文本出现了!但是我从 initWithCoder 添加的其他子视图没有。为什么是这样?

.h 文件

@interface TWTimerView : UIView

@property (strong, nonatomic) IBOutlet UIButton *clock;

@property (strong, nonatomic) UIImageView *circle;
@property (strong, nonatomic) UIImageView *pointer;
@property (strong, nonatomic) UIImageView *tick;

@property (strong, nonatomic) UIImageView *circleGlow;
@property (strong, nonatomic) UIImageView *pointerGlow;
@property (strong, nonatomic) UIImageView *tickGlow;

@end

.m 文件

@implementation TWTimerView

-(id)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];
    if(self) {

        self.backgroundColor = [UIColor clearColor];

        _circle  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle"]];
        _pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer"]];
        _tick    = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick"]];

        [_clock addSubview:_circle];
        [_clock addSubview:_pointer];
        [_clock addSubview:_tick];

    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        TWTimerView *view= [[[NSBundle mainBundle] loadNibNamed:@"TimerView" owner:self options:nil] objectAtIndex:0];
        [self addSubview:view];

    }
    return self;
}

@end

谢谢!

4

2 回答 2

2

这是我要做的:如果您不需要使用框架,请删除该initWithFrame方法并使用另一种方法来帮助您加载视图,就像这样。

+ (id)loadViewFromNIBFile {
    NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"TimerView" owner:self options:nil];
    //You may want to assert array only contains one element here
    TWTimerView * view = (TWTimerView *)[array objectAtIndex:0];
    NSAssert([view isKindOfClass:TWTimerView.class], @"Unexpected class");
    [view _setDefaultComponents];
    return view;
}

- (void)_setDefaultComponents {
    self.backgroundColor = [UIColor clearColor];
    _circle  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle"]];
    _pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer"]];
    _tick    = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick"]];
    [_clock addSubview:_circle];
    [_clock addSubview:_pointer];
    [_clock addSubview:_tick];
}

调用[TWTimerView loadViewFromNIBFile]以获取您的视图的实例。

于 2013-07-23T12:39:31.323 回答
0

您的 TWTimerView 有一个笔尖,其中您有一个 UIButton?你有一个 UIView-Subclass,就是在你的 UIButton 中添加三个 UIImageView,你为什么要这样做?!

为什么不继承 UIButton 并在那里设置 UIImageViews?

我根本会把它改成这样:

@implementation TWTimerView

    - (id)initWithFrame:(CGRect)frame
    {

        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [[[NSBundle mainBundle] loadNibNamed:@"TimerView" owner:self options:nil] objectAtIndex:0];
            self.backgroundColor = [UIColor clearColor];

            _circle  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle"]];
            _pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer"]];
            _tick    = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick"]];

            [_clock addSubview:_circle];
            [_clock addSubview:_pointer];
            [_clock addSubview:_tick];

        }
        return self;
    }

@end
于 2013-07-23T12:31:28.333 回答