【发布时间】:2013-07-23 12:09:40
【问题描述】:
我有一个 UIView 的子类,我是从一个 NIB 文件加载的。我的 NIB 文件,包含一个 UIButton 和一个 IBOutlet,称为 clock
@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
谢谢!
【问题讨论】:
-
你是在向 uibutton 添加图像视图吗???
-
是的,我的 UIButton 是我的 _clock。我像这样添加它们:[_clock addSubview:_image];
-
在调用
initWithFrame时,您实际上是在添加TWTimerView作为TWTimerView的子视图吗?还是 TimerView 只是一个带有某种内容子视图的常规 .xib 文件? -
我不明白您为什么要分配两次视图...您确定调用了
initWithCoder方法吗? -
根据我的测试它不是,你真的需要使用
initWithFrame方法还是我可以定义另一个方法来为你加载视图?
标签: ios objective-c uiview uibutton subclass