我有一个 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
谢谢!